/// <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>
        /// 4 MINUTE BUTTON
        /// WILL RUN 8 TURNS
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmd4Min_Click(object sender, System.EventArgs e)
        {
            try
            {
                Object objData = new Object();
                for (int i = 1; i <= 35; i++)
                {
                    objData = "RECD";
                    if (i % 4 == 0)
                    {
                        ParsedData dataOutParse = new ParsedData(dataOut);
                        objData = algorithm(dataOutParse.getDemand(), dataOutParse);
                        if (output || i >= 33)
                        {
                            txtDataRx.Text += Environment.NewLine;
                        }
                    }
                    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 || i >= 33)
                        {
                            txtDataRx.Text += Environment.NewLine + szData;
                            txtDataRx.SelectionStart = txtDataRx.Text.Length;
                            txtDataRx.ScrollToCaret();
                        }

                    if (i % 4 != 0 & szData != "END")
                    {
                        dataOut[dataCount] = szData;
                        dataCount++;

                        if (dataCount == 3)
                        {
                            ParsedData dataOutParse = new ParsedData(dataOut);
                            frm2.chartUpdate(dataOutParse.getDemand());
                            frm2.profitUpdate(dataOutParse.getProfit());
                            frm2.profitChartUpdate(dataOutParse.getDemand(), dataOutParse.getProfit());
                            dataCount = 0;
                        }
                    }
                    cmd30Seconds.Enabled = false;
                    cmd4Min.Enabled = false;
                    cmd30Min.Enabled = false;
                    cmdRead.Enabled = false;

                        if (szData == "END")
                        {
                            cmdClose_Click(sender, e);
                        }

                }
            }
            catch (System.Net.Sockets.SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
        /// <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>
        /// 30 SECOND BUTTON
        /// SENDS ONE TURN TO THE SERVER
        /// REQUIRES A MANUAL CONTROL
        /// IF LOOP IS ENABLED, THIS BUTTON WILL RUN UNTIL SERVER THROWS END AT IT
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void cmd30Seconds_Click_1(object sender, System.EventArgs e)
        {
            try
            {
                Object objData = new Object();
                for (int i = 1; i <= 3 ; i++)
                {
                    objData = "RECD";
                    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 || szData == "END")
                    {
                        txtDataRx.Text += Environment.NewLine + szData;
                        txtDataRx.SelectionStart = txtDataRx.Text.Length;
                        txtDataRx.ScrollToCaret();
                    }

                    if (szData != "END")
                    {
                        dataOut[dataCount] = szData;
                        dataCount++;

                        if (dataCount == 3)
                        {
                            ParsedData dataOutParse = new ParsedData(dataOut);
                            frm2.chartUpdate(dataOutParse.getDemand());
                            frm2.profitUpdate(dataOutParse.getProfit());
                            frm2.profitChartUpdate(dataOutParse.getDemand(), dataOutParse.getProfit());
                            dataCount = 0;
                        }
                    }

                    if (!loop)
                    {
                        cmd30Seconds.Enabled = false;
                        cmd4Min.Enabled = false;
                        cmd30Min.Enabled = false;
                        cmdRead.Enabled = false;
                    }

                    //IF THE SERVER ENDS, THROW STOP
                    if (szData == "END")
                    {
                        cmdClose_Click(sender, e);
                    }

                    if (loop == true && i == 3)
                    {
                        cmdSendData_Click(sender, e);
                        i = 0;
                        /*count++;
                        if (count > 50)
                        {
                            txtDataRx.Text = "";
                            count = 0;
                        }*/
                    }
                }
            }
            catch (System.Net.Sockets.SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }