   constructor: (reflector, @port) ->
     # ...
     @reflectors = {}
+    @clients = []
+
+  addClient: (client) ->
+    @clients.push(client)
+
+  removeClient: (client) ->
+    if @clients.indexOf(client) >= 0
+      @clients.splice(@clients.indexOf(client), 1)
+
+  clientsAheadOf: (client) ->
+    index = @clients.indexOf(client)
+
+    if index < MAX_CLIENTS
+      -1
+    else
+      index - MAX_CLIENTS
 
   clearReflectors: ->
     @reflectors = {}


    @wsServer.on "request", @onRequest

  onRequest: (request) =>
    console.log "[server] new request from"
    console.log request.remoteAddress

    # todo - check request.origin maybe?
    try
      connection = request.accept("scenevr", request.origin)
    catch 
      # doesnt want scenevr - drop the client
      return

    reflector = @reflectors[request.resource]

    if !reflector
      console.log "[server] 404 scene not found '#{request.resource}'"
      request.reject()
      return

    console.log "[server] client requested '#{request.resource}'"
    
    observer = new Observer(connection, reflector)
    reflector.addObserver(observer)

    connection.on "message", (message) =>
      if message.type is "utf8"
        observer.recieveMessage(message.utf8Data)

    connection.on "close", (reasonCode, description) =>
      console.log "[server] Peer " + connection.remoteAddress + " disconnected."
      reflector.removeObserver(observer)

module.exports = WebsocketServer
WSFrame  = require('websocket').frame
WSRouter = require('websocket').router
HTTP = require("http")
MAX_CLIENTS = 8

debug = (message) ->
  console.log (new Date()) + " " + message
