net = require( 'net' );

var clients = [];

var gameServer = net.createServer( function ( socket ) {
  socket.setTimeout( 60 * 1000 ); // 1 minute timeout
  socket.setNoDelay( true );
  socket.name = socket.remoteAddress + ":" + socket.remotePort;
  socket.id = clients.length + 1; // start ids at 1

  // set up some listeners for this socket
  socket.on( 'data', function( data ) {
    //console.log( "data of length " + data.readUInt16BE( 1 ) + ":|" + data.toString() + "|")
    if ( data == '<policy-file-request/>\0' ) {
      socket.write( getCrossDomainPolicy() + '\0' );
    } else {
      relay( data, socket );
    }
  } );

  socket.on( 'end', function() {
    clients.splice( clients.indexOf( socket ), 1 );
    // relay leave event
    console.log( "client " + socket.name + " left the game." );
  } );

  // acknowledge this socket's connection
  clients.push( socket );
  console.log( "connected to client " + socket.name );

  var clientID = new Buffer( [ 255, socket.id ] );
  socket.write( clientID ); // send the client ID
  var socketJoin = new Buffer( [ 254, socket.id ] );

  // broadcast this client's joining to every other client
  clients.forEach( function( client ) {
    if ( client == socket ) return;
    try {
      if ( client.writable ) {
        client.write( socketJoin );
      } else {
        console.log( client.id + " is not writable." );
      }
      var clientAdd = new Buffer( [ 254, client.id ] );
      if ( socket.writable ) {
        socket.write( clientAdd );
      } else {
        clients.splice( clients.indexOf( socket ), 1 );
        console.log( socket.id + " is not writable." );
      }
    } catch( err ) {
      console.log( "error broadcasting client joining : " + err );
    }
  } );

  function relay( data, sender ) {
    clients.forEach( function( client ) {
      if ( client == sender ) return;
      var posID = new Buffer( [ 253, sender.id ] );
      try {
        if ( client.writable ) {
          client.write( posID );
          client.write( data );
          console.log( posID.toString() + "   |   " + data.length + " | " + data.readUInt16BE(1) + " | " + data.toString() );
        } else {
          clients.splice( clients.indexOf( client ), 1 );
          console.log( client.id + " is not writable." );
        }
      } catch ( err ) {
        console.log( "cannot send message to " + client.id + ": " + err );
      }
    } );
  }
} ).listen( process.env.PORT || 5000, null );

var policyServer = net.createServer( function ( socket ) {
  socket.on( 'data', function( data ) {
    if ( data == "<policy-file-request/>\0" ) {
      socket.write( getCrossDomainPolicy() + '\0' );
      socket.end();
    }
  } );
} ).listen( 10000, null );

function getCrossDomainPolicy() {
  return policy = '<cross-domain-policy><allow-access-from domain="*" to-ports="*" /></cross-domain-policy>';
}

@@ -26,19 +26,49 @@
 var 
-client
+MAX_INSTANCES = 10;%0Avar instance
 s = %5B
+ %5B%5D 
 %5D;%0A%0A
@@ -270,16 +270,69 @@
 otePort;
+%0A%0A  var instance = instances%5B instances.length - 1 %5D;
 %0A  socke
@@ -338,23 +338,24 @@
 et.id = 
-clients
+instance
 .length 
@@ -378,16 +378,209 @@
 s at 1%0A%0A
+  if ( socket.id %3E 4 ) %7B // create new instance%0A    instances.push( %5B%5D );%0A    instance = instances%5B instances.length - 1 %5D;%0A    socket.id = 1;%0A  %7D%0A%0A  socket.instanceId = instances.length - 1;%0A%0A
   // set
@@ -938,31 +938,83 @@
 ion() %7B%0A    
-clients
+var instance = instances%5B socket.instanceId %5D;%0A    instance
 .splice( cli
@@ -1002,39 +1002,40 @@
 nstance.splice( 
-clients
+instance
 .indexOf( socket
@@ -1184,23 +1184,46 @@
 ction%0A  
-clients
+instances%5B socket.instanceId %5D
 .push( s
@@ -1503,23 +1503,24 @@
 lient%0A  
-clients
+instance
 .forEach
@@ -1871,39 +1871,40 @@
  else %7B%0A        
-clients
+instance
 .splice( clients
@@ -1888,39 +1888,40 @@
 nstance.splice( 
-clients
+instance
 .indexOf( socket
@@ -2134,23 +2134,75 @@
 ) %7B%0A    
-clients
+var instance = instances%5B sender.instanceId %5D;%0A    instance
 .forEach
@@ -2577,23 +2577,24 @@
         
-clients
+instance
 .splice(
@@ -2598,15 +2598,16 @@
 ce( 
-clients
+instance
 .ind

implement instances WIP