static async void loadController()
        {
            Guid deviceId = Settings.Default.DeviceId;

            if (deviceId == Guid.Empty)
            {
                Console.WriteLine("No saved device ID found");
                Settings @default = Settings.Default;
                Guid     guid     = Guid.NewGuid();
                deviceId          = guid;
                @default.DeviceId = guid;
                Settings.Default.Save();
            }
            else
            {
                //Console.WriteLine("Previously saved device ID found");
            }

            transportFactory = new TransportFactory();
            devicePairing    = new DevicePairing(deviceId, transportFactory, new SpcApiWrapper());
            IPlayerNotificationProvider playerNotificationProvider = new PlayerNotificationProvider();
            IDeviceListener             uPnPDeviceListener         = new UPnPDeviceListener(new NetworkInfoProvider(), transportFactory);
            IDeviceDiscovery            uPnPDeviceDiscovery        = new UPnPDeviceDiscovery(transportFactory, uPnPDeviceListener);
            IDeviceDiscovery            tvDiscovery = new TvDiscovery(uPnPDeviceDiscovery, new TcpWebTransport(TimeSpan.FromSeconds(5)));

            deviceController = new DeviceController(tvDiscovery, devicePairing, playerNotificationProvider, new DeviceSettingProvider());
            //Console.WriteLine("Device discovery starting");
            deviceController.StartDiscovery();

            bool previousDeviceAsync = await deviceController.ConnectToPreviousDeviceAsync();

            if (!previousDeviceAsync)
            {
                Console.WriteLine("No previously paired TV found.");
                loadDevice(deviceController);
                checkNetwork(deviceController);
            }
            else
            {
                Console.WriteLine("Previously paired TV found.");
                verifyNetwork();
            }
        }
 private void upnpDiscovery_DeviceConnected(object sender, DeviceInfoEventArgs e)
 {
     if (e.DeviceInfo.DeviceType != "urn:dial-multiscreen-org:device:dialreceiver:1")
     {
         Logger   instance   = Logger.Instance;
         object[] deviceType = new object[] { e.DeviceInfo.DeviceType, e.DeviceInfo.DeviceAddress };
         instance.LogMessageFormat("upnpDiscovery_DeviceConnected Device is not Dial Receiver, TV type is {0}, with address: {1}.", deviceType);
         return;
     }
     TvDiscovery.Tv2014InitInfo tv2014Data = TvDiscovery.GetTv2014Data(e.DeviceInfo);
     if (tv2014Data != null)
     {
         Console.WriteLine("Model 2014 Samsung TV found.");
         this.foundTvs.Add(tv2014Data);
         return;
     }
     Console.WriteLine("Pre 2014 Samsung TV found");
     this.devicePool.Add(e.DeviceInfo);
 }
        private static async Task ConnectToDevice(object param)
        {
            try
            {
                try
                {
                    Console.WriteLine("Connecting to selected TV now");
                    DeviceInfo deviceInfo = param as DeviceInfo;
                    if (deviceInfo == null)
                    {
                        return;
                    }
                    else if (!TvDiscovery.IsTv2014(deviceInfo))
                    {
                        Console.WriteLine("TV model is not supported. Must be atleast 2014 model.");
                        return;
                    }
                    else if (await deviceController.TryToConnect(deviceInfo.DeviceAddress.Host))
                    {
                        Console.WriteLine("Selected TV connected successfully");
                        goToRemoteControl(deviceController);
                    }
                    else
                    {
                        switch (await deviceController.Connect(deviceInfo))
                        {
                        case DeviceController.ConnectResult.PinPageAlreadyShown:
                        {
                            //goToRemoteControl(deviceController);
                            //Console.WriteLine("Another device is connecting. Please try again.");
                            //goToDiscovery(deviceController);
                            return;
                        }

                        case DeviceController.ConnectResult.SocketException:
                        {
                            Console.WriteLine("Socket exception during connection attempt.");
                            deviceController.RefreshDiscovery();
                            return;
                        }

                        case DeviceController.ConnectResult.OtherException:
                        {
                            Console.WriteLine("Unknown exception during connection attempt.");
                            goToDiscovery(deviceController);
                            return;
                        }

                        default:
                        {
                            Console.WriteLine("Enter PIN from TV within 30 seconds.");
                            string pin = Console.ReadLine();

                            if (await OnPair(pin))
                            {
                                Console.WriteLine("Pairing attempt succeeded. Launching remote control interface now.");
                                goToRemoteControl(deviceController);
                            }
                            else
                            {
                                Console.WriteLine("Error during pairing attempt. Please try again.");
                                goToDiscovery(deviceController);
                            }
                            break;
                        }
                        }
                    }
                }
                catch (Exception exception2)
                {
                    Console.WriteLine("Unknown exception occurred during pairing attempt. Please try again.");
                    goToDiscovery(deviceController);
                }
            }
            finally
            {
                Console.WriteLine("deviceController.Connect call complete");
            }
        }