public void CreateRegistry(Registry registry)
 {
   //  _magDb.Registries.      //Add(registry);
     try
     {
         //using (_magDb)
         //{
          var   _magDb = new MagLink_engineEntities();
             _magDb.Entry(registry).State = registry.ID == 0 ? EntityState.Added : EntityState.Modified;
             _magDb.SaveChanges();
            // _magDb.Dispose();
         //}
     }
     catch (Exception ex)
     {
         ErrorHandler._ErrorHandler.LogError(ex, "Error saving changes to registry");
     }
     
 }
        public Registry GetRegistry()
        {

            Registry registry = new Registry();
            
            try
            {
                //using (_magDb)
                //{
                  var   _magDb = new MagLink_engineEntities();
                    return _magDb.Registries.FirstOrDefault(p => p.ID == 1); 
                    
                //}
            }
            catch (Exception ex)
            {
                ErrorHandler._ErrorHandler.LogError(ex, "Error accessing the registry table");
                
            }

            return registry;
        }
        static void Main(string[] args)
        {
            //application state trackers
            bool shutdown = false;
            bool serverstarted = false;
            bool clientstarted = false;

            os_util = new OSUtil();

            //Intialize your settings and settings file
            //Settings. intSettings = new Settings();

            String path = Directory.GetCurrentDirectory();
            String setFile = @"\settings.xml";
            String ComPath = path + setFile;
           
            var registry = new Registry();
            registry = _repository.GetRegistry();

            registry.Status = "ON";
            registry.ErrorMessage = "";
            registry.ErrorState = "";
            registry.HeartBeat = System.DateTime.Now;
            _repository.CreateRegistry(registry);
           
            // set timer to mantain the heartBeat now

            _timerHeartBeat.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
            _timerHeartBeat.Interval = 15000;
            _timerHeartBeat.Start();
            

            //Check for config file and if it doesn't exist then create it

            if (File.Exists(ComPath))
            {
                Settings intSet = Settings._instance.Deserialize(ComPath);
                Settings._instance = intSet;
            }
            else
            {
                //Might want to end here or do something with no config
                Settings._instance.Serialize(ComPath, Settings._instance);
                // to shut down just call os_server.Stop();
            }
            if (Settings._instance.Type.ToUpper().Trim() == "SERVER")
            {
                os_server = new OSServer(_repository);

                bool started = os_server.Start();
                if (!started)
                {
                    ErrorHandler._ErrorHandler.LogInfo(os_server.GetLastError()+ "-Failed to Start Server.");
                    
                }
                else
                {
                    ErrorHandler._ErrorHandler.LogInfo(string.Format("Server started successfully.\nRunning on Port:{0} and IP:{1}", Settings._instance.LocalPort, Settings._instance.LocalIPAddress));
                    
                    serverstarted = true;
                }
            }

            if (Settings._instance.Type.ToUpper().Trim() == "CLIENT")
            {
               var os_client = new OSClient(_repository);
                os_client.fileWatcherStart();

               bool connected = os_client.Connect(Settings._instance.RemoteIPAddress, Convert.ToInt32(Settings._instance.RemotePort));
               
                if (!connected)
               {
                   ErrorHandler._ErrorHandler.LogInfo("Failed to Start Client.");
                   
               }
                else
                {
                    ErrorHandler._ErrorHandler.LogInfo(string.Format("Client started successfully.\nRunning on Port:{0} and IP:{1}", Settings._instance.RemoteIPAddress, Settings._instance.RemotePort));
                    //connected = true;
                    clientstarted = true;
                    os_client.StarStopTimer("Stop");
                   
                  
                    os_client.SendMessages();
                   os_client.StarStopTimer("START");

                 }
            }
            
            
            while (!shutdown)
            {
                string userinput = Console.ReadLine();

                if (!string.IsNullOrEmpty(userinput))
                {
                    switch (os_util.ParseCommand(userinput))
                    {
                        case OSUtil.os_cmd.OS_EXIT:
                            {
                                if (serverstarted)
                                {
                                    registry.Status = "OFF";
                                    registry.HeartBeat = System.DateTime.Now;
                                    _repository.CreateRegistry(registry);
                                    os_server.Stop();
                                }

                                if (clientstarted)
                                {
                                    os_client.DisConnect();
                                    registry.Status = "OFF";
                                    registry.HeartBeat = System.DateTime.Now;
                                    _repository.CreateRegistry(registry);
                                }
                                
                                shutdown = true;
                                break;
                            }
                        
                    }

                }
            }


        }