Proper handling of waits in stream_close and socket_close