/// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static void Main(string[] args)
        {
            ArgumentsDictionary argsDict = ProcessArguments(args);

            //this logger is not being used at the moment
            logger = new Logger((string)argsDict["Log"]);

            Settings.InitSettingsFromFile((string) argsDict["ConfigFile"]);

            HomeService homeService = new HomeService(logger);

            homeService.Start(null);

            while (!serviceExited)
            {
                System.Threading.Thread.Sleep(10000);
            }

            Environment.Exit(homeService.ExitCode);
        }
        private void Shutdown()
        {
            this.StopAllModules();
            this.DisposeAllScouts();
            this.Dispose();
            if(homeService!=null) 
                homeService.Stop();


            this.runningScouts = null;
            this.runningModules = null;
            this.runningModulesStates = null;
            this.registeredPorts = null;
            this.registeredPorts = null;
            this.config = null;
            this.allAddinTokens = null;
            this.policyEngine = null;
            //this.gadgetListener = null;
            this.guiService = null;
            this.homeService = null;
            this.infoService = null;
            this.discoveryHelperService = null;
            this.heartbeatService = null;
            this.random = null;

            GC.Collect();

            //finally close the logger object
            if (logger != null)
                logger.Close();
        }
        /// <summary>
        /// Starts the home service that connects us to the cloud using the gatekeeper
        /// </summary>
        private void InitHomeService()
        {
            if (string.IsNullOrWhiteSpace(Settings.HomeId))
            {
                logger.Log("Cannot start home service without a configured home id. What is going on?");
                return;
            }

            HomeOS.Shared.Gatekeeper.Settings.HomeId = Settings.HomeId;
            HomeOS.Shared.Gatekeeper.Settings.ServiceHost = Settings.GatekeeperURI;

            homeService = new Gatekeeper.HomeService(logger);
            homeService.Start(null);
        }
        private void Initialize(string[] arguments)
        {
            safeToGoOnline = false;
            guiService = null;
            homeService = null; 

            //initialize various data structures
            random = new Random();
            runningModules = new Dictionary<VModule, ModuleInfo>();
            runningScouts = new Dictionary<string, Tuple<ScoutInfo, IScout>>();
            runningModulesStates = new Dictionary<VModule, VModuleState>();
            registeredPorts = new Dictionary<VPort, VModule>();

            //this initializes the settings to what was default (in the code)
            Settings.Initialize();

            ArgumentsDictionary argsDict = ProcessArguments(arguments);

            //were we supplied a non-default configuration directory?
            if (!DEFAULT_COMMAND_LINE_ARG_VAL.Equals((string)argsDict["ConfigDir"]))
                Settings.SetParameter("ConfigDir", (string) argsDict["ConfigDir"]);
           
            //this overwrites the settings to what was in the configuration file
            config = new Configuration(Settings.ConfigDir);
            config.ParseSettings();

            //now, overwrite the settings with those on the command line
            foreach (var parameter in argsDict.Keys)
            {
                if (parameter.Equals("Help"))
                    continue;

                if (!DEFAULT_COMMAND_LINE_ARG_VAL.Equals((string)argsDict[parameter]))
                    config.UpdateConfSetting(parameter, argsDict[parameter]);
            }


            //initialize the logger
            logger = InitLogger(Settings.LogFile);
            logger.Log("Platform initialized");

            //set the logger for config and read remaining config fines
            config.SetLogger(logger);
            config.ReadConfiguration();

            //initialize the policy enginer
            policyEngine = new PolicyEngine(logger);
            policyEngine.Init(config);

            //rebuild the addin tokens
            this.rebuildAddInTokens();

            homeStoreInfo = new HomeStoreInfo(logger);
            _consoleHandler = new PlatformConsoleCtrlHandlerDelegate(ConsoleEventHandler);
            SetConsoleCtrlHandler(_consoleHandler, true);
        }