         )
 UserProfiles = new UserProfilesGlobal()
 
-io = socketio.listen(5000)
-io.set('log level', 2)
-
-handleErrors = (fn) ->
-    return ->
-        try
-            return fn.apply(this, arguments)
-        catch err
-            console.log('Exception: ' + err)
-
-io.sockets.on('connection', (socket) ->
-    socket.on('joinGlobalLobby', handleErrors((req, cb) ->
+class GameServer
+    start: ->
+        @io = socketio.listen(5000)
+        @io.set('log level', 2)
+        @io.sockets.on('connection', (socket) =>
+            fns = _.functions(this)
+            fns = _.reject(fns, (fn) -> fn is 'start' or fn[0] is '_')
+            socket.on(fn, this._event(this[fn], socket)) for fn in fns
+        )
+
+    _event: (fn, socket) ->
+        return ->
+            args = _.toArray(arguments)
+            args.unshift(socket)
+            try
+                return fn.apply(this, args)
+            catch err
+                logError(err)
+        
+    joinGlobalLobby: (socket, req, cb) =>
         if req is null
             cb(GlobalLobby.addAnon(socket))
         else
-            UserProfiles.get(req.profile_url, handleErrors((err, profile) ->
+            UserProfiles.get(req.profile_url, (err, profile) ->
                 return if err
                 req.league = profile.league
                 player = new Player(socket, req)
                 cb(GlobalLobby.addPlayer(player))
-            ))
-    ))
-    socket.on('getUserProfile', handleErrors((profile_url, cb) ->
+            )
+
+    getUserProfile: (socket, profile_url, cb) =>
         UserProfiles.get(profile_url, cb)
-    ))
-    socket.on('createLobby', handleErrors((req, cb) ->
+
+    createLobby: (socket, req, cb) =>
         UserProfiles.get(req.profile_url, (err, profile) ->
             return if err
             req.league = profile.league

sqlite3 = require('sqlite3')
util = require('util')
_ = require('underscore')

class Player
    constructor: (socket, request) ->

            GlobalLobby.removeByID(player.id)
            cb(players)
        )

    sendChat: (socket, text) =>
        if GlobalLobby.isPresent(socket.id)
            GlobalLobby.sendChat(socket.id, text)
        else
            LobbyManager.sendChat(socket.id, text)

    exitLobby: (socket) =>
        LobbyManager.removePlayer(socket.id)

    disconnect: (socket) =>
        GlobalLobby.removeByID(socket.id)
        LobbyManager.removePlayer(socket.id)

new GameServer().start() if require.main is module
