Restructure main code into sockets.js and server-side events