/// <summary>
        /// MANUAL COMMAND ENTRY TO THE SERVER
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void cmdSendData_Click(object sender, System.EventArgs e)
        {
            try
            {
                Object objData = txtData.Text;
                byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString ());
                m_socWorker.Send (byData);
                byte[] buffer = new byte[1024];
                int iRx = m_socWorker.Receive(buffer);
                char[] chars = new char[iRx];

                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
                System.String szData = new System.String(chars);
                if (output)
                {
                    txtDataRx.Text += Environment.NewLine + Environment.NewLine + szData;
                    txtDataRx.SelectionStart = txtDataRx.Text.Length;
                    txtDataRx.ScrollToCaret();
                }
                dataOut[3] = szData;
                ParsedData dataOutParse = new ParsedData(dataOut);
                frm2.gridUpdate(dataOutParse.getConfig());
                frm2.profitUpdate(dataOutParse.getProfit());
                cmd30Seconds.Enabled = true;
                cmd4Min.Enabled = true;
                cmd30Min.Enabled = true;
                cmdRead.Enabled = true;

            }
            catch(System.Net.Sockets.SocketException se)
            {
                MessageBox.Show (se.Message );
            }
        }
        /// <summary>
        /// OUR BUSTED ALGORITHM
        /// </summary>
        /// <param name="demandIn"></param>
        /// <param name="dataIn"></param>
        /// <returns></returns>
        private string algorithm(int[,] demandIn, ParsedData dataIn)
        {
            if (algorithmCheck == true)
            {
                int[,] dist = dataIn.getDist();
                int[,] config = dataIn.getConfig();
                int[,] configR = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
                int[] profit = dataIn.getProfit();
                int[] NAdist = { dist[0, 0], dist[1, 0], dist[2, 0] };
                int[] EUdist = { dist[0, 1], dist[1, 1], dist[2, 1] };
                int[] APdist = { dist[0, 2], dist[1, 2], dist[2, 2] };
                double output;
                double derivative;
                int error;
                storePoints(NAServers, NAdist);
                storePoints(EUServers, EUdist);
                storePoints(APServers, APdist);
                double[,] slopes = genSlope();

                ///////WEB/////////
                //NA Web
                error = demandIn[0, 2] - (config[0, 0] * 200);
                derivative = (demandIn[0, 2] + slopes[0, 0]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {
                    if (output > 0)
                    {
                        if (output > (reqTraffic[0]))
                        {
                            configR[0, 0]++;
                            config[0, 0]++;
                        }
                    }
                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (NAwd < config[0, 0])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[0]))
                            {
                                if (config[0, 0] > 1)
                                {
                                    configR[0, 0]--;
                                    config[0, 0]--;
                                    NAwd++;
                                }
                            }
                        }
                    }
                }

                //EU Web
                error = demandIn[1, 2] - (config[0, 1] * 200);
                derivative = (demandIn[1, 2] + slopes[0, 1]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {
                    if (output > 0)
                    {
                        if (output > (reqTraffic[0]))
                        {
                            configR[0, 1]++;
                            config[0, 1]++;
                        }
                    }
                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (EUwd < config[0, 1])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[0]))
                            {
                                if (config[0, 1] > 1)
                                {
                                    configR[0, 1]--;
                                    config[0, 1]--;
                                    EUwd++;
                                }
                            }
                        }
                    }
                }

                //AP Web
                error = demandIn[2, 2] - (config[0, 2] * 200);
                derivative = (demandIn[2, 2] + slopes[0, 2]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {
                    if (output > 0)
                    {
                        if (output > (reqTraffic[0]))
                        {
                            configR[0, 2]++;
                            config[0, 2]++;

                        }
                    }

                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (APwd < config[0, 2])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[0]))
                            {
                                if (config[0, 2] > 1)
                                {
                                    configR[0, 2]--;
                                    config[0, 2]--;
                                    APwd++;
                                }
                            }
                        }
                    }
                }

                //////////////////JAVA
                //NA Java
                error = dist[0, 0] - (config[1, 0] * 500);
                derivative = (demandIn[0, 2] + slopes[1, 0]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {
                    if (output > 0)
                    {
                        if (output > (reqTraffic[1]))
                        {
                            configR[1, 0]++;
                            config[1, 0]++;
                        }
                    }
                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (NAjd < config[1, 0])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[1]))
                            {
                                if (config[1, 0] > 0)
                                {
                                    configR[1, 0]--;
                                    config[1, 0]--;
                                    NAjd++;
                                }
                            }
                        }
                    }
                }

                //EU Java
                error = dist[0, 1] - (config[1, 1] * 500);
                derivative = (dist[0, 1] + slopes[1, 1]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {
                    if (output > 0)
                    {
                        if (output > (reqTraffic[1]))
                        {
                            configR[1, 1]++;
                            config[1, 1]++;
                        }
                    }
                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (EUjd < config[1, 1])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[1]))
                            {
                                if (config[1, 1] > 1)
                                {
                                    configR[1, 1]--;
                                    config[1, 1]--;
                                    EUjd++;
                                }
                            }
                        }
                    }
                }

                //AP Java
                error = dist[0, 2] - (config[1, 2] * 500);
                derivative = (dist[0, 2] + slopes[1, 2]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {
                    if (output > 0)
                    {
                        if (output > (reqTraffic[1]))
                        {

                            configR[1, 2]++;
                            config[1, 2]++;
                        }
                    }
                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (APjd < config[1, 2])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[1]))
                            {
                                if (config[1, 2] > 1)
                                {
                                    configR[1, 2]--;
                                    config[1, 2]--;
                                    APjd--;
                                }
                            }
                        }
                    }
                }

                ////DATABASES///////
                //NA DB
                error = dist[1, 0] - (config[2, 0] * 1200);
                derivative = (dist[1, 0] + slopes[2, 0]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {
                    if (output > 0)
                    {
                        if (output > (reqTraffic[2]))
                        {
                            configR[2, 0]++;
                            config[2, 0]++;
                        }
                    }
                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (NAdd < config[2, 0])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[2]))
                            {
                                if (config[2, 0] > 1)
                                {
                                    configR[2, 0]--;
                                    config[2, 0]--;
                                    NAdd++;
                                }
                            }
                        }
                    }
                }

                //EU DB
                error = dist[1, 1] - (config[2, 1] * 1200);
                derivative = (dist[1, 1] + slopes[2, 1]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {

                    if (output > 0)
                    {
                        if (output > (reqTraffic[2]))
                        {
                            configR[2, 1]++;
                            config[2, 1]++;
                        }
                    }
                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (EUdd < config[1, 1])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[2]))
                            {
                                if (config[2, 1] > 1)
                                {
                                    configR[2, 1]--;
                                    config[2, 1]--;
                                    EUdd++;
                                }
                            }
                        }
                    }
                }

                //AP DB
                error = dist[1, 2] - (config[2, 2] * 1200);
                derivative = (dist[1, 2] + slopes[2, 2]) / 2;
                output = error + derivative;

                if (turnCount % 6 == 0)
                {

                    if (output > 0)
                    {
                        if (output > (reqTraffic[2]))
                        {
                            configR[2, 2]++;
                            config[2, 2]++;
                        }
                    }
                }

                if ((turnCount + 2) % 8 == 0)
                {
                    if (APdd < config[2, 2])
                    {
                        if (output < 0)
                        {
                            if (output < (reqTraffic[2]))
                            {
                                if (config[2, 2] > 1)
                                {
                                    configR[2, 2]--;
                                    config[2, 2]--;
                                    APdd++;
                                }
                            }
                        }
                    }
                }

                turnCount++;
                /* if ((turnCount) % 6 == 0)
                 {
                      NAwd = 0;
                      NAjd = 0;
                      NAdd = 0;
                      EUwd = 0;
                      EUjd = 0;
                      EUdd = 0;
                      APwd = 0;
                      APjd = 0;
                      APdd = 0;
                 }*/

                return ParsedData.parseServerControlArray(configR);
            }
            else
            {
                return "CONTROL 0 0 0 0 0 0 0 0 0";
            }
        }
        /// <summary>
        /// CONNECTION TO SOCKET
        /// USE BUILT IN .NET SOCKET LIBRARY
        /// CONNECTS TO SOCKET
        /// THROWS INIT, RECD, AND START
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void cmdConnect_Click(object sender, System.EventArgs e)
        {
            cmdConnect.Enabled = false;
            cmdClose.Enabled = true;
            try
            {
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 15; j++)
                    {
                        NAServers[i, j] = 0;
                        EUServers[i, j] = 0;
                        APServers[i, j] = 0;
                    }
                }
                //create a new client socket ...
                m_socWorker = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                String szIPSelected  = txtIPAddress.Text;
                String szPort = txtPort.Text;
                int  alPort = System.Convert.ToInt16 (szPort,10);

                System.Net.IPAddress	remoteIPAddress	 = System.Net.IPAddress.Parse(szIPSelected);
                System.Net.IPEndPoint	remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, alPort);
                m_socWorker.Connect(remoteEndPoint);

                for (int i = 0; i < 3; i++)
                {
                    Object objData = new Object();
                    if (i == 0)
                    {
                        objData = "INIT Carrot Island Development Team";
                    }
                    else if (i == 1)
                    {
                        objData = "RECD";
                    }

                    else
                    {
                        objData = "START";
                    }
                    byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
                    m_socWorker.Send(byData);
                    byte[] buffer = new byte[1024];
                    int iRx = m_socWorker.Receive(buffer);
                    char[] chars = new char[iRx];

                    System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                    int charLen = d.GetChars(buffer, 0, iRx, chars, 0);
                    System.String szData = new System.String(chars);

                    if (objData == "RECD")
                    {

                        cost = ParsedData.parse1DArray(szData);
                        setupCost();
                    }
                    if (objData == "START")
                    {
                        dataOut[0] = "";
                        dataOut[1] = "";
                        dataOut[2] = "";
                        dataOut[3] = szData;
                        ParsedData dataOutParse = new ParsedData(dataOut);
                        frm2.gridUpdate(dataOutParse.getConfig());
                    }
                    txtDataRx.Text += Environment.NewLine + szData;
                    txtDataRx.SelectionStart = txtDataRx.Text.Length;
                    txtDataRx.ScrollToCaret();

                }
                cmd30Seconds.Enabled = true;
                cmd4Min.Enabled = true;
                cmd30Min.Enabled = true;
                cmdSendData.Enabled = true;
                loopButton.Enabled = true;
                cmdRead.Enabled = true;
                cmdSendData.Enabled = true;

            }
            catch (System.Net.Sockets.SocketException se)
            {
                MessageBox.Show ( se.Message );
            }
        }