Contracts are only part of the issue. Relationships matter more... The only job in my life I've ever been 'fired' from involved precisely this issue.
I was contracted to write some simulation dynamics engine. I placed clear headers in each source file indicating that although I was liberally licensing the code to my employer for his use, I retained full ownership of the source code, and I reserved the right to file a claim if my employer turned around and sold or misused the software I wrote without my consent.
This came as a shock to my employer as he thought I was being unreasonable. The fact is that the law was on my side. When he realized that, he fired me.