add window beforeunload to handle disconnects