public void Run()
            {
                try
                {
                    exitCode = ExitCode.ErrorServerNotStarted;
                    RpiWiFiOpcUaServer().Wait();
                    Console.WriteLine("Server started. Press Ctrl-C to exit...");
                    exitCode = ExitCode.ErrorServerRunning;
                }
                catch (Exception ex)
                {
                    Utils.Trace("ServiceResultException:" + ex.Message);
                    Console.WriteLine("Exception: {0}", ex.Message);
                    exitCode = ExitCode.ErrorServerException;
                    return;
                }

                ManualResetEvent quitEvent = new ManualResetEvent(false);

                try
                {
                    Console.CancelKeyPress += (sender, eArgs) => {
                        quitEvent.Set();
                        eArgs.Cancel = true;
                    };
                }
                catch
                {
                }

                // wait for timeout or Ctrl-C
                quitEvent.WaitOne(serverRunTime);

                if (server != null)
                {
                    Console.WriteLine("Server stopped. Waiting for exit...");

                    using (RpiWiFiOpcUaServer _server = server)
                    {
                        // Stop status thread
                        server = null;
                        status.Wait();
                        // Stop server and dispose
                        _server.Stop();
                    }
                }

                exitCode = ExitCode.Ok;
            }
            private async Task RpiWiFiOpcUaServer()
            {
                //ApplicationInstance.MessageDlg = new ApplicationMessageDlg();
                ApplicationInstance application = new ApplicationInstance();

                application.ApplicationName   = "UA Core Rpi WiFi";
                application.ApplicationType   = ApplicationType.Server;
                application.ConfigSectionName = "Opc.Ua.RpiWiFiOpcUa";

                // load the application configuration.
                ApplicationConfiguration config = await application.LoadApplicationConfiguration(false);

                // check the application certificate.
                bool haveAppCertificate = await application.CheckApplicationInstanceCertificate(false, 0);

                if (!haveAppCertificate)
                {
                    throw new Exception("Application instance certificate invalid!");
                }

                if (!config.SecurityConfiguration.AutoAcceptUntrustedCertificates)
                {
                    config.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(CertificateValidator_CertificateValidation);
                }

                // start the server.
                server = new RpiWiFiOpcUaServer();

                await application.Start(server);

                // start the status thread
                status = Task.Run(new Action(StatusThread));

                // print notification on session events
                server.CurrentInstance.SessionManager.SessionActivated += EventStatus;
                server.CurrentInstance.SessionManager.SessionClosing   += EventStatus;
                server.CurrentInstance.SessionManager.SessionCreated   += EventStatus;
            }