Refactoring and fixing of the TCP socket wrapper.