Extra checks around reading/writing to socket.

The read path is implemented this way due to the wording around CFReadStreamHasBytesAvailable(). It will return TRUE if it cannot determine whether bytes are atually available or not. If it does this, we will get into an infinite loop if CFReadStreamRead() also returns 0 bytes read. This change should not make matters any worse -- if the stream is at the end (closed by the other side), then closing it locally is a fine thing to do. There's no need to wait for the read event callback.