Fix issue #333 - Unhandled socket state error can crash server

Closes #333