Helper functions taken from AnyEvent::Socket