handle non-blocking sockets properly