tcp: Fix a few problems in blocking mode (mainly when the client is blocking).
tcp: code cleanup