   _handleSocketMessage: (connection, message) ->
 
     @log 'got socket message', message
-    @_proccessMessage connection, message
-
-
-  _handleSendNotification: (req, res) ->
-
-    @log 'got hit on send'
-    res.send 'NotificationWorker is OK'
-
+    @_proccessSocketMessage connection, message
+
+
+  _proccessSocketMessage: (connection, message) ->
+
+    if connection.id not in @_verifiedConnections
+
+      @log 'connection not verified, checking...'
+      [ err, parsedMessage ] = @_parseMessage message
+
+      if err or not parsedMessage?.auth?
+        @_dropConnection connection
+
+      else
+        { clientId, username, groupName } = parsedMessage.auth
+
+        if /^guest-/.test username
+          return @_dropConnection connection
+
+        @_bongo.models.JSession.one { clientId, username, groupName }, (err, session) =>
+          if err or not session
+            @_dropConnection connection
+          else
+            @_verifyConnection connection, groupName, username
+            @_ackConnection connection
+
+    else
+      @_ackConnection connection
+
+
+  _ackConnection: (connection) ->
+
+    connection.write '{ "auth": "ok" }'
+
+
+  # --- socker connection management
+
+  _verifyConnection: (connection, group, user) ->
+
+    @_verifiedConnections.push connection.id
+    @_verifiedRoutes.push [ group, user, connection.id ]
+    @log 'connection verified now, welcome!', connection.url
+
+    return connection
+
+
+  _dropConnection: (connection) ->
+
+    connection.close()
+    return  unless @_connections[connection.id]
+
+    @_verifiedConnections = @_verifiedConnections.filter (connectionId) ->
+      connection.id isnt connectionId
+
+    @_verifiedRoutes = @_verifiedRoutes.filter (route) ->
+      [ ... , connectionId ] = route
+      connection.id isnt connectionId
+
+    delete @_connections[connection.id]
+    @log 'connection dropped. goodbye!', connection.url
+
+    return null
+
+
+  # --- generic parser for stringified json
 
   _parseMessage: (message) ->
 

      return [ { error, message: 'Not a valid JSON' } ]

    return [ null, message ]


  _proccessMessage: (connection, message) ->

    if connection.id not in @_verifiedConnections

      @log 'connection not verified, checking...'
      [ err, parsedMessage ] = @_parseMessage message

      if err or not parsedMessage?.auth?
        @_dropConnection connection

      else
        { clientId, username, groupName } = parsedMessage.auth

        if /^guest-/.test username
          return @_dropConnection connection

        @_bongo.models.JSession.one { clientId, username, groupName }, (err, session) =>
          if err or not session
            @_dropConnection connection
          else
            @_verifyConnection connection, "#{groupName}:#{username}"
            connection.write 'welcome'

    else
      connection.write 'ok'


  _verifyConnection: (connection, route) ->

    @_verifiedConnections.push connection.id
    @_verifiedRoutes.push "#{route}:#{connection.id}"
    @log 'connection verified now, welcome!', connection.url

    return connection


  _dropConnection: (connection) ->

    connection.close()
    return  unless @_connections[connection.id]

    @_verifiedConnections = @_verifiedConnections.filter (connectionId) ->
      connection.id isnt connectionId

    @_verifiedRoutes = @_verifiedRoutes.filter (route) ->
      [group, username, connectionId] = route.split ':'
      connection.id isnt connectionId

    delete @_connections[connection.id]
    @log 'connection dropped. goodbye!', connection.url

    return null

    @_app.use helmet()
    @_app.use cors()

    @_app.get  '/notify',                  @_handleStatusRequest.bind this
    @_app.post '/dispatcher_notify_user',  @_handleSendNotification 'user'
    @_app.post '/dispatcher_notify_group', @_handleSendNotification 'group'

    return @_app


  _startServer: ->
