reorganise socket handling