Check for double close/open in socket impl

Check to state of socket when doing open/read/write/close to prevent double-open
and double-close issues with the socket implementation.

Fixes #228