Improved TCP server, refactoring