Refactoring web socket related code