Fixing TCP socket handling