Refactored TCP server code heavily