better socket handling