public static IApplication Run()
        {
            ConnectionSettings connectionSettings = CreateLocalConnectionSettings();

            if (CheckConnection(connectionSettings))
            {
                throw new ApplicationStartupException(ErrorMessageFormatter.HostApplicationIsAlreadyLaunched());
            }
            IApplication application = null;

            switch (CurrentRuntype)
            {
            case Runtype.Inplace:
                application = RunInplace(false);
                break;

            case Runtype.Application:
                application = RunApplication();
                break;

            case Runtype.Service:
                application = RunService();
                break;

            default:
                application = RunInplace(false);
                break;
            }
            return(application);
        }
        public static IApplication Connect(ConnectionSettings connectionSettings)
        {
            VerifyManagedConnector();
            if (!CheckConnection(connectionSettings))
            {
                throw new ApplicationStartupException(ErrorMessageFormatter.UnableToConnectToHostApplication());
            }
            IApplication application = ConnectInternal(connectionSettings);

            return(application);
        }
        private static void WaitForStartup(ConnectionSettings connectionSettings)
        {
            // checkPeriod (ms) * iterations = totally wait time (ms)
            // 200 (ms) * 20 = 4000 (ms) = 4 (sec)
            const int checkPeriod = 200;
            const int iterations  = 20;

            for (int i = 0; i < iterations; i++)
            {
                if (CheckConnection(connectionSettings))
                {
                    return;
                }
                Thread.Sleep(checkPeriod);
            }
            throw new ApplicationStartupException(ErrorMessageFormatter.UnableToConnectToHostApplication());
        }