Clean-up and fixes for daemon execution.
Fixed issue with port not being written correctly.
More fixes to port handling.
Some threading fixes.