In general as a consultant you can assert certain rights for yourself. don't be afraid to request changes to the contract.<p>Also as a contractor you are VERY different from an employee and the resulting IP rights are quite different. While ALL an employee does during their employment usually belongs to the company (even inventions done while at home sleeping :) the situation is very different for a contractor.<p>For example it is a standard practice that NOT ALL of the code that you write for them is theirs, but only "business specific" parts.
You should retain the rights to the generic parts of the code, all successful consultancies build a code libraries that they can reuse.
We usually release such things as open source but not always, we do keep the rights to ourselves though. Customer just gets a full royalty free license to do whatever they want.<p>Same with inventions.
If during the term of the agreement while working on their project you invent a super-duper-fast sorting algorithm and implement it in their project, it still doesn't mean the algorithm is theirs.<p>The implementation is (and only if its business specific) but the idea is yours and you can go as far as filing a patent for it (I'm quite against software patents, but still). Again, the client gets all the necessary rights to use it in-this-particular-implementation but that is.