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

Closes #333 
Move this up to 296 so that we don't get any messages from the socket after we remove listeners on the sync.