protected void ConnectToServer()
        {
            if (_server != null)
            {
                return;
            }

            if (!IsPINValid())
            {
                return;
            }

            _currentUtility = libraryPicker.SelectedItem as DiscoveredPairingUtility;
            if (_currentUtility == null)
            {
                return;
            }

            string serviceID = _currentUtility.ServiceID;

            if (!BonjourManager.DiscoveredServers.ContainsKey(serviceID))
            {
                MessageBox.Show(LocalizedStrings.LibraryCouldNotBeLocated, LocalizedStrings.LibraryConnectionErrorTitle, MessageBoxButton.OK);
                return;
            }

            _libraryService = BonjourManager.DiscoveredServers[serviceID];

            string hostname    = _libraryService.IPAddresses[0].ToString();
            string pairingCode = string.Format("{0:0000}{0:0000}{0:0000}{0:0000}", pinTextBox.IntValue.Value);

            _server = new DACPServer(hostname, _libraryService.Port, pairingCode);

            UpdateWizardItem(true);

            _log.Info("Connecting to server with ID '{0}' at {1}:{2}...", serviceID, _server.Hostname, _server.Port);
            HandleServerConnection(_server.ConnectAsync());
        }
        protected async void ConnectToServer()
        {
            if (_server != null)
            {
                return;
            }

            if (!HasValidData())
            {
                return;
            }

            string host;
            int    port;

            ParseHostname(out host, out port);

            string pairingCode = string.Format("{0:0000}{0:0000}{0:0000}{0:0000}", pinTextBox.IntValue.Value);

            try
            {
                _server = new DACPServer(host, port, pairingCode);
            }
            catch
            {
                MessageBox.Show(LocalizedStrings.LibraryConnectionErrorBody, LocalizedStrings.LibraryConnectionErrorTitle, MessageBoxButton.OK);
                _server = null;
                UpdateWizardItem(true);
                return;
            }

            UpdateWizardItem(true);

            var result = await _server.ConnectAsync();

            switch (result)
            {
            case ConnectionResult.Success:
                // Save the server connection info
                ServerConnectionInfo info = new ServerConnectionInfo();
                info.Name          = _server.LibraryName;
                info.PairingCode   = _server.PairingCode;
                info.LastHostname  = _server.Hostname;
                info.LastIPAddress = _server.Hostname;
                info.LastPort      = _server.Port;

                // Get the service ID for Bonjour
                // In iTunes 10.1 and later, the service name comes from ServiceID (parameter aeIM).
                // In foo_touchremote the service name is the same as the database ID (parameter mper).
                // In MonkeyTunes, the service ID is not available from the database query. TODO.
                if (_server.MainDatabase.ServiceID > 0)
                {
                    info.ServiceID = _server.MainDatabase.ServiceID.ToString("x16").ToUpper();
                }
                else
                {
                    info.ServiceID = _server.MainDatabase.PersistentID.ToString("x16").ToUpper();
                }

                ServerManager.AddServerInfo(info);
                ServerManager.ChooseServer(info);

                Hide();

                NavigationManager.GoToFirstPage();
                break;

            case ConnectionResult.InvalidPIN:
                MessageBox.Show(LocalizedStrings.LibraryPINErrorBody, LocalizedStrings.LibraryPINErrorTitle, MessageBoxButton.OK);
                _server = null;
                UpdateWizardItem(true);
                break;

            case ConnectionResult.ConnectionError:
                MessageBox.Show(LocalizedStrings.LibraryConnectionErrorBody, LocalizedStrings.LibraryConnectionErrorTitle, MessageBoxButton.OK);
                _server = null;
                UpdateWizardItem(true);
                break;
            }
        }
        private async void HandleServerConnection(Task <ConnectionResult> task)
        {
            var result = await task;

            switch (result)
            {
            case ConnectionResult.Success:
                _log.Info("Successfully connected to server at {0}:{1}", _server.Hostname, _server.Port);

                // Notify the pairing utility so it can close
                if (_currentUtility != null)
                {
                    _currentUtility.SendPairedNotification(_server.PairingCode);
                }

                // Save the server connection info
                ServerConnectionInfo info = new ServerConnectionInfo();
                info.Name          = _server.LibraryName;
                info.ServiceID     = _libraryService.Name;
                info.PairingCode   = _server.PairingCode;
                info.LastHostname  = _libraryService.Hostname;
                info.LastIPAddress = _server.Hostname;
                info.LastPort      = _server.Port;

                _server = null;

                ServerManager.AddServerInfo(info);
                ServerManager.ChooseServer(info);

                Hide();

                NavigationManager.GoToFirstPage();
                break;

            case ConnectionResult.InvalidPIN:
                MessageBox.Show(LocalizedStrings.LibraryPINErrorBody, LocalizedStrings.LibraryPINErrorTitle, MessageBoxButton.OK);
                _server = null;
                UpdateWizardItem(true);
                break;

            case ConnectionResult.ConnectionError:
                // Check whether there are any other IP addresses we could try
                var ipStrings = _libraryService.IPAddresses.Select(ip => ip.ToString()).ToList();
                var ipIndex   = ipStrings.IndexOf(_server.Hostname);
                if (ipIndex >= 0 && ipIndex < (ipStrings.Count - 1))
                {
                    ipIndex++;
                    string nextIP = ipStrings[ipIndex];
                    _server.Hostname = nextIP;
                    _server.Port     = _libraryService.Port;
                    _log.Info("Retrying connection on new IP: {0}:{1}", _server.Hostname, _server.Port);
                    HandleServerConnection(_server.ConnectAsync());
                    return;
                }

                // No other IPs, so we can't do anything else
                // TODO: Display error
                _server = null;
                UpdateWizardItem(true);
                break;
            }
        }