Extra checks around reading/writing to socket.
Merge pull request #76 from kgoodier/fix/socket-safety

Extra checks around reading/writing to socket.