private void UpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                if (updaterTimer != null)
                {
                    updaterTimer.Stop();
                    updaterTimer = null;
                }
                Task<bool> task = Updater.CheckUpdates();
                task.Wait();
                if (InstanceProvider.GetHostInfo().GetGlobalValue("CheckUpdates") == "true" && updater != null && task.Result)
                {
                    if (Updater.VersionChanged)
                    {
                        InstanceProvider.GetServiceLogger().AppendLine("Version change detected! Restarting server(s) to apply update...");
                        if (Stop(_hostControl))
                        {
                            Start(_hostControl);
                        }
                    }

                    updaterTimer = new System.Timers.Timer((updater.GetNextOccurrence(DateTime.Now) - DateTime.Now).TotalMilliseconds);
                    updaterTimer.Elapsed += UpdateTimer_Elapsed;
                    updaterTimer.Start();
                }
            }
            catch (Exception ex)
            {
                _log.Error("Error in UpdateTimer_Elapsed", ex);
            }
        }
        private void ValidSettingsCheck()
        {
            bool validating = true;
            while (validating)
            {
                if (InstanceProvider.GetHostInfo().GetServerInfos().Count() < 1)
                {
                    throw new Exception("No Servers Configured");
                }
                else
                {
                    foreach (ServerInfo server in InstanceProvider.GetHostInfo().GetServerInfos())
                    {
                        foreach (ServerInfo compareServer in InstanceProvider.GetHostInfo().GetServerInfos())
                        {
                            if (server != compareServer)
                            {
                                if (server.ServerExeName.Equals(compareServer.ServerExeName) || server.ServerPath.Equals(compareServer.ServerPath) || server.GetServerProp("server-port").Value.Equals(compareServer.GetServerProp("server-port").Value) || server.GetServerProp("server-portv6").Value.Equals(compareServer.GetServerProp("server-portv6").Value) || server.GetServerProp("server-name").Value.Equals(compareServer.GetServerProp("server-name").Value))
                                {
                                    InstanceProvider.GetServiceLogger().AppendLine($"Duplicate server settings detected for: {server.ServerName}");
                                }
                            }
                        }
                    }
                    foreach (var server in InstanceProvider.GetHostInfo().GetServerInfos())
                    {
                        if (Updater.VersionChanged)
                        {
                            ReplaceBuild(server);
                        }
                        if (server.ServerExeName.Value != "bedrock_server.exe" && File.Exists(server.ServerPath.Value + "\\bedrock_server.exe") && !File.Exists(server.ServerPath.Value + "\\" + server.ServerExeName.Value))
                        {
                            File.Copy(server.ServerPath.Value + "\\bedrock_server.exe", server.ServerPath.Value + "\\" + server.ServerExeName.Value);
                        }

                        if (!File.Exists(server.ServerPath.Value + "\\" + server.ServerExeName.Value))
                        {
                            ReplaceBuild(server);
                        }


                    }
                    if (Updater.VersionChanged)
                        Updater.VersionChanged = false;
                    else
                        validating = false;
                }
            }

        }
        private void ClientHostThread()
        {
            try
            {
                ClientHost.StartListening(int.Parse((string)InstanceProvider.GetHostInfo().GetGlobalValue("ClientPort")));
                InstanceProvider.GetServiceLogger().AppendLine("Before process.WaitForExit()");
                ParentProcessUtilities.GetParentProcess().WaitForExit();
                InstanceProvider.GetServiceLogger().AppendLine("After process.WaitForExit()");

                InstanceProvider.GetServiceLogger().AppendLine("Stop socket service");
                ClientHost.client.Close();
                GC.Collect();
            }
            catch (ThreadAbortException abort)
            {
                InstanceProvider.GetServiceLogger().AppendLine($"WCF Thread reports {abort.Message}");
            }
        }
        public BedrockService()
        {
            CurrentServiceStatus = ServiceStatus.Starting;
            if (LoadInit())
            {
                //Program.ServiceLogger = new Program.ServiceLogger(localHost);
                TCPServerThread = new Thread(new ThreadStart(ClientHostThread));
                TCPServerThread.Start();
                try
                {
                    foreach (ServerInfo Server in InstanceProvider.GetHostInfo().GetServerInfos())
                    {
                        BedrockServer srv = new BedrockServer(Server);
                        shed = CrontabSchedule.TryParse(InstanceProvider.GetHostInfo().GetGlobalValue("BackupCron"));
                        if (InstanceProvider.GetHostInfo().GetGlobalValue("BackupEnabled") == "true" && shed != null)
                        {
                            cronTimer = new System.Timers.Timer((shed.GetNextOccurrence(DateTime.Now) - DateTime.Now).TotalMilliseconds);
                            cronTimer.Elapsed += CronTimer_Elapsed;
                            cronTimer.Start();
                        }

                        updater = CrontabSchedule.TryParse(InstanceProvider.GetHostInfo().GetGlobalValue("UpdateCron"));
                        if (InstanceProvider.GetHostInfo().GetGlobalValue("CheckUpdates") == "true" && updater != null)
                        {
                            updaterTimer = new System.Timers.Timer((updater.GetNextOccurrence(DateTime.Now) - DateTime.Now).TotalMilliseconds);
                            updaterTimer.Elapsed += UpdateTimer_Elapsed;
                            InstanceProvider.GetServiceLogger().AppendLine($"Updates Enabled, will be checked in: {((float)updaterTimer.Interval / 1000)} seconds.");
                            updaterTimer.Start();
                        }
                        InstanceProvider.GetConfigManager().WriteJSONFiles(Server);
                        InstanceProvider.GetConfigManager().SaveServerProps(Server, false);
                        bedrockServers.Add(srv);
                    }
                    CurrentServiceStatus = ServiceStatus.Started;
                }
                catch (Exception e)
                {
                    InstanceProvider.GetServiceLogger().AppendLine($"Error Instantiating BedrockServiceWrapper: {e.StackTrace}");
                }
            }
        }
        private void CronTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                if (cronTimer != null)
                {
                    cronTimer.Stop();
                    cronTimer = null;
                }
                if ((string)InstanceProvider.GetHostInfo().GetGlobalValue("BackupEnabled") == "true" && shed != null)
                {
                    Backup();

                    cronTimer = new System.Timers.Timer((shed.GetNextOccurrence(DateTime.Now) - DateTime.Now).TotalMilliseconds);
                    cronTimer.Elapsed += CronTimer_Elapsed;
                    cronTimer.Start();
                }
            }
            catch (Exception ex)
            {
                _log.Error("Error in BackupTimer_Elapsed", ex);
            }
        }