Implement handshaking for TCP connections

Implement handshaking for TCP connections and handle connections on a separate thread.