Allow port number to be zero to use a random free port.

Keep track of the threads started.
Wait for threads to finish when stopping for a clean shutdown.
Fix compiler warnings.
Fix some issues reported by Coverity static analysis tool.
Copyright 2013 F-Secure Corporation.