Refactor tcp server.