Refactored socket.io functions