Extra checks around socket read/write operations.

Protect against 0-byte reads and writes. Protect against unusual
behavior of the CFStream apis around returning 0 bytes read/written.