Fix various bugs with tcp-server implementation.