private void btnStartAll_click(object sender, EventArgs e)
 {
     //lock (BotSettings.Instance)
     //{
     ConnectionCheck.Reset();
     // Start All
     foreach (
         var row in
         botGrid.Rows.Cast <DataGridViewRow>().Where(row => (bool)row.Cells["isEnabled"].Value))
     {
         BotSettings.Instance.Bots[row.Index].Start(checkBoxForce.Checked);
     }
     //}
 }
            private void HandleMsg(string msg)
            {
                // Message Example:
                // PID:CMD DATA
                // 1234:GameLeft 25-09-1985 18:27:00
                Debug.WriteLine("Recieved: " + msg);

                try
                {
                    var pid = msg.Split(':')[0];
                    var cmd = msg.Substring(pid.Length + 1).Split(' ')[0];
                    int x;
                    msg = msg.Substring(((x = pid.Length + cmd.Length + 2) >= msg.Length ? 0 : x));

                    var b = BotSettings.Instance.Bots.FirstOrDefault(f => (f.Demonbuddy != null && f.Demonbuddy.Proc != null) && f.Demonbuddy.Proc.Id == Convert.ToInt32(pid));
                    if (b == null)
                    {
                        Send("Error: Unknown process");
                        return;
                    }

                    switch (cmd)
                    {
                    case "Initialized":
                        b.AntiIdle.Stats = new BotStats
                        {
                            LastGame    = DateTime.Now.Ticks,
                            LastPulse   = DateTime.Now.Ticks,
                            PluginPulse = DateTime.Now.Ticks,
                            LastRun     = DateTime.Now.Ticks
                        };
                        b.AntiIdle.LastStats     = DateTime.Now;
                        b.AntiIdle.State         = IdleState.CheckIdle;
                        b.AntiIdle.IsInitialized = true;
                        Send("Roger!");
                        break;

                    case "GameLeft":
                        b.ProfileSchedule.Count++;
                        if (b.ProfileSchedule.Current.Runs > 0)
                        {
                            Logger.Instance.Write(b, "Runs completed ({0}/{1})", b.ProfileSchedule.Count, b.ProfileSchedule.MaxRuns);
                        }
                        else
                        {
                            Logger.Instance.Write(b, "Runs completed {0}", b.ProfileSchedule.Count);
                        }

                        if (b.ProfileSchedule.IsDone)
                        {
                            var newprofile = b.ProfileSchedule.GetProfile;
                            Logger.Instance.Write("Next profile: {0}", newprofile);
                            Send("LoadProfile " + newprofile);
                        }
                        else
                        {
                            Send("Roger!");
                        }
                        break;

                    case "UserStop":
                        b.Status         = string.Format("User Stop: {0:d-m H:M:s}", DateTime.Now);
                        b.AntiIdle.State = IdleState.UserStop;
                        Logger.Instance.Write(b, "Demonbuddy stopped by user");
                        Send("Roger!");
                        break;

                    case "StartDelay":
                        var delay = new DateTime(long.Parse(msg));
                        b.AntiIdle.StartDelay = delay.AddSeconds(10);
                        b.AntiIdle.State      = IdleState.StartDelay;
                        Send("Roger!");
                        break;

                    // Giles Compatibility
                    case "ThirdpartyStop":
                        b.Status         = string.Format("Thirdparty Stop: {0:d-m H:M:s}", DateTime.Now);
                        b.AntiIdle.State = IdleState.UserStop;
                        Logger.Instance.Write(b, "Demonbuddy stopped by Thirdparty");
                        Send("Roger!");
                        break;

                    case "GilesPause":
                        b.AntiIdle.State = IdleState.UserPause;
                        Send("Roger!");
                        break;

                    case "AllCompiled":
                        Send(b.Demonbuddy.ForceEnableAllPlugins ? "ForceEnableAll" : "ForceEnableYar");
                        break;

                    case "CrashTender":
                        b.Demonbuddy.CrashTender();
                        Send("Roger!");
                        break;

                    case "CheckConnection":
                        ConnectionCheck.CheckValidConnection(silent: true);
                        Send("Roger!");
                        break;

                    // Unknown command reply
                    default:
                        Send("Unknown command!");
                        Logger.Instance.WriteGlobal("Unknown command recieved: " + msg);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Send("Internal server error: " + ex.Message);
                    Logger.Instance.WriteGlobal(ex.ToString());
                }
            }
            private void HandleMsg(string msg)
            {
                // Message Example:
                // PID:CMD DATA
                // 1234:GameLeft 25-09-1985 18:27:00
                Debug.WriteLine("Recieved: " + msg);
                try
                {
                    string pid = msg.Split(':')[0];
                    string cmd = msg.Substring(pid.Length + 1).Split(' ')[0];
                    int    x;
                    msg = msg.Substring(((x = pid.Length + cmd.Length + 2) >= msg.Length ? 0 : x));

                    BotClass b =
                        BotSettings.Instance.Bots.FirstOrDefault(
                            f =>
                            (f.Demonbuddy != null && f.Demonbuddy.Proc != null) &&
                            f.Demonbuddy.Proc.Id == Convert.ToInt32(pid));
                    if (b == null)
                    {
                        Send("Error: Unknown process");
                        StatFailed++;
                        return;
                    }

                    long nowTicks = DateTime.UtcNow.Ticks;

                    switch (cmd)
                    {
                    case "Initialized":
                        b.AntiIdle.Stats = new BotStats
                        {
                            LastGame    = nowTicks,
                            LastPulse   = nowTicks,
                            PluginPulse = nowTicks,
                            LastRun     = nowTicks
                        };
                        b.AntiIdle.LastStats     = DateTime.UtcNow;
                        b.AntiIdle.State         = IdleState.CheckIdle;
                        b.AntiIdle.IsInitialized = true;
                        b.AntiIdle.InitAttempts  = 0;
                        Send("Roger!");
                        break;

                    case "GameLeft":
                        b.ProfileSchedule.Count++;
                        if (b.ProfileSchedule.Current.Runs > 0)
                        {
                            Logger.Instance.Write(b, "Runs completed ({0}/{1})", b.ProfileSchedule.Count,
                                                  b.ProfileSchedule.MaxRuns);
                        }
                        else
                        {
                            Logger.Instance.Write(b, "Runs completed {0}", b.ProfileSchedule.Count);
                        }

                        if (b.ProfileSchedule.IsDone)
                        {
                            string newprofile = b.ProfileSchedule.GetProfile;
                            Logger.Instance.Write(b, "Next profile: {0}", newprofile);
                            Send("LoadProfile " + newprofile);
                        }
                        else
                        {
                            Send("Roger!");
                        }
                        break;

                    case "NewDifficultyLevel":
                        Logger.Instance.Write(b, "Sending DifficultyLevel: {0}",
                                              b.ProfileSchedule.Current.DifficultyLevel);
                        Send("DifficultyLevel " + (int)b.ProfileSchedule.Current.DifficultyLevel);
                        break;

                    case "UserStop":
                        b.Status         = string.Format("User Stop: {0:d-m H:M:s}", DateTime.UtcNow);
                        b.AntiIdle.State = IdleState.UserStop;
                        Logger.Instance.Write(b, "Demonbuddy stopped by user");
                        Send("Roger!");
                        break;

                    case "StartDelay":
                        var delay = new DateTime(long.Parse(msg));
                        b.AntiIdle.StartDelay = delay.AddSeconds(60);
                        b.AntiIdle.State      = IdleState.StartDelay;
                        Send("Roger!");
                        break;

                    // Giles Compatibility
                    case "ThirdpartyStop":
                        b.Status         = string.Format("Thirdparty Stop: {0:d-m H:M:s}", DateTime.UtcNow);
                        b.AntiIdle.State = IdleState.UserStop;
                        Logger.Instance.Write(b, "Demonbuddy stopped by Thirdparty");
                        Send("Roger!");
                        break;

                    case "TrinityPause":
                        b.AntiIdle.State = IdleState.UserPause;
                        Logger.Instance.Write(b, "Trinity Pause Detected");
                        Send("Roger!");
                        break;

                    case "AllCompiled":
                    {
                        Logger.Instance.Write(b, "Check Force Enable Plugins? {0}", b.Demonbuddy.ForceEnableAllPlugins);
                        Send(b.Demonbuddy.ForceEnableAllPlugins ? "ForceEnableAll" : "ForceEnableYar");
                        break;
                    }

                    case "CrashTender":
                        if (Settings.Default.UseKickstart && File.Exists(msg))
                        {
                            b.Demonbuddy.CrashTender(msg);
                        }
                        else
                        {
                            b.Demonbuddy.CrashTender();
                        }
                        Send("Roger!");
                        break;

                    case "CheckConnection":
                        ConnectionCheck.CheckValidConnection(true);
                        Send("Roger!");
                        break;

                    case "D3Exit":
                        Send("Shutdown");
                        b.Diablo.Proc.CloseMainWindow();
                        break;

                    // Unknown command reply
                    default:
                        Send("Unknown command!");
                        Logger.Instance.WriteGlobal("Unknown command recieved: " + msg);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    StatFailed++;
                    Send("Internal server error: " + ex.Message);
                    DebugHelper.Exception(ex);
                }
            }