Handle "not connected" errors

This stops the script from attempting to write to closed websockets, which causes websocket.js to throw an error and exit the process. Instead it emits an error and continues.