/// <summary>
        /// Connects to the TFS server specified by <see cref="IServerManagement.CurrentUri"/>.
        /// </summary>
        public async Task ConnectAsync()
        {
            await HandleServerError(async() =>
            {
                IsConnecting = true;
                try
                {
                    var serverUrl      = Servers.CurrentUri;
                    bool serverChanged = !UriEqualityComparer.Instance.Equals(serverUrl, _explorer.Server?.Uri);

                    if (_explorer.Server != null)
                    {
                        _explorer.Server.ConnectionStatusChanged -= Server_ConnectionStatusChanged;
                    }

                    await _explorer.ConnectAsync(serverUrl);
                    _explorer.Server.ConnectionStatusChanged += Server_ConnectionStatusChanged;

                    var currentProjectName = ProjectName;
                    ProjectNames.Clear();
                    ProjectNames.AddRange((await _explorer.GetTeamProjectsAsync()).Select(n => n.Name));

                    // Restore project name on reconnect.
                    if (ProjectNames.Contains(currentProjectName) && !serverChanged)
                    {
                        ProjectName = currentProjectName;
                    }

                    if (serverChanged)
                    {
                        ProjectName = null;
                    }

                    IsConnected = true;

                    Servers.Add(serverUrl);
                    OnConnectionSucceeded(serverUrl);

                    await Task.CompletedTask;
                }
                finally
                {
                    IsConnecting = false;
                }
            });
        }
        private async Task HandleServerError(Func <Task> action)
        {
            string errorMessage = null;

            try
            {
                await action();

                Status = null;
            }
            catch (TestObjectNotFoundException e)
            {
                errorMessage = e.Message;
            }
            catch (TeamFoundationServiceUnavailableException e)
            {
                errorMessage = e.Message;
            }
            catch (Exception e) when(e.InnerException != null &&
                                     e.InnerException.GetType() == typeof(TeamFoundationServiceUnavailableException))
            {
                errorMessage = e.InnerException.Message;
            }
            catch (AdalException e)
            {
                errorMessage = e.Message;
            }
            finally
            {
                if (errorMessage != null)
                {
                    Status      = errorMessage;
                    IsConnected = false;
                    ProjectNames.Clear();
                }
            }
        }