Reimplement connect_tcp_socket using a class

There were too many nested functions in the implementation, so the
code started to be not so clear.