Improve socket handling, and re-enqueue events if sending fails.