improved socket handling, now supporting reconnects