Bugfix: Check stream when close socket

We need to check if the socket has been released.

In general, the "close_stream" function may be called repeatedly in one loop iteration.