var fs=require('fs');
var express =require('express');
var app=express();

var http1= require('http').Server(app);
var http2= require('http').Server(app);
var envMonitorIO=require('socket.io')(http1);
var resMonitorIO=require('socket.io')(http2);
var serverURL='www.grovemonitor-jamesokbo.c9users.io:8081';

var mongoose= require('mongoose');
var configDB= require('./server/config/database.js');
mongoose.connect(configDB.url);

//MONGOOSE SCHEMAS

//Mongoose ResMonitor and its sensor readings' schemas
var ResMonitor=require('./server/models/resMonitor');
var PhReading=require('./server/models/phReading');
//TODO: Missing ResMonitor Sensor readings
//TODO: Mongoose EnvMonitor and its sensor readings' schemas

//Arrays of connected monitors
var envMonitors=[];
var resMonitors=[];
var envMonitorIDs=[];
var resMonitorIDs=[];

//Environment variables
var mainIDPath='';
var connectedToServer=false;

var socketToServer = require('socket.io-client')(serverURL);

//TODO: Add Actuators arrays and schemas
//TODO: ADD SOCKETIO COMMUNICATIONS
//TODO: Add SocketIO communication protocol with the server
socketToSever.on('connect',function(){
  fs.readFile(mainIDPath,'utf8',function(err,data){
    if(err){
      console.log(err);
      throw err;
    }
    socketToServer.emit('identification', {mainID: data}, function(err,res){
      if(err){
        console.log(err);
        throw err;
      }
      if(res.status){
        if(res.new){
          fs.writeFile(mainIDPath, res.id, 'utf8', function(err){
            if(err){
              console.log(err);
              throw err;
            }
            mainID=res.id;
            socketToServer.disconnect();
          });
        }
        else{
          connectedToServer=true;
        }
      }
      else{
        if(res.error){
          console.log(res.error);
        }
        socketToServer.disconnect();
      }
    });
  });
});
//TODO: Add SocketIO communication protocol with the envMonitor
//TODO: Add SocketIO communication protocol with the resMonitor
//TODO: Add SocketIO communication protocol with the actuators

ResMonitor.update({},{$set:{status:false}},{multi:true},function(err,res){
  if(err){
    throw err;
  }
  if(res.ok==1){
    EnvMonitor.update({},{$set:{status:false}},{multi:true},function(err,res){
      if(err){
        throw err;
      }
      console.log(res.ok +' '+res.nModified);
      if(res.ok==1){
        http1.listen(8080,function(){
          console.log('envMonitor socketserver running @ port: 8080');
        });
        http2.listen(8081,function(){
          console.log('resMonitor socketserver running @ port: 8081');
        });
      }
    });
  }
});

@@ -11,24 +11,53 @@
 uire('fs');%0A
+var async= require('async');%0A
 var express 
@@ -73,24 +73,24 @@
 'express');%0A
-
 var app=expr
@@ -741,260 +741,8 @@
 mas%0A
-%0A//Arrays of connected monitors%0Avar envMonitors=%5B%5D;%0Avar resMonitors=%5B%5D;%0Avar envMonitorIDs=%5B%5D;%0Avar resMonitorIDs=%5B%5D;%0A%0A//Environment variables%0Avar mainIDPath='';%0Avar connectedToServer=false;%0A%0Avar socketToServer = require('socket.io-client')(serverURL);%0A%0A
 //TO
@@ -814,16 +814,17 @@
 CATIONS%0A
+%0A
 //TODO: 
@@ -879,802 +879,58 @@
 ver%0A
-socketToSever.on('connect',function()%7B%0A  fs.readFile(mainIDPath,'utf8',function(err,data)%7B%0A    if(err)%7B%0A      console.log(err);%0A      throw err;%0A    %7D%0A    socketToServer.emit('identification', %7BmainID: data%7D, function(err,res)%7B%0A      if(err)%7B%0A        console.log(err);%0A        throw err;%0A      %7D%0A      if(res.status)%7B%0A        if(res.new)%7B%0A          fs.writeFile(mainIDPath, res.id, 'utf8', function(err)%7B%0A            if(err)%7B%0A              console.log(err);%0A              throw err;%0A            %7D%0A            mainID=res.id;%0A            socketToServer.disconnect();%0A          %7D);%0A        %7D%0A        else%7B%0A          connectedToServer=true;%0A        %7D%0A      %7D%0A      else%7B%0A        if(res.error)%7B%0A          console.log(res.error);%0A        %7D%0A        socketToServer.disconnect();%0A      %7D%0A    %7D);%0A  %7D);%0A%7D
+var socketToServer = require('./socketToServer.js'
 );%0A/

Update server.js