How would I check if a tcp port is available in a thread? I tried using a background worker thread, and it worked, but updated rather slowly. I want to report progress to my UI thread. Is this a correct way?
    BackgroundWorker authStatus = new BackgroundWorker {WorkerReportsProgress = true};
    authStatus.DoWork += AuthStatusWork;
    authStatus.ProgressChanged += AuthStatusProgressChanged;
    public void AuthStatusWork(object sender, EventArgs e)
    {
        var thread = sender as BackgroundWorker;
        using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            socket.SendTimeout = 3;
            while (true)
            {
                try
                {
                    socket.Connect(auth[0], Convert.ToInt32(auth[1]));
                    if (thread != null) thread.ReportProgress(1);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode != SocketError.ConnectionRefused &&
                        ex.SocketErrorCode != SocketError.TimedOut) continue;
                    if (thread != null) thread.ReportProgress(0);
                }
            }
        }
    }
    public void AuthStatusProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        switch (e.ProgressPercentage)
        {
            case 0:
                AuthStatus.Foreground = Brushes.Red;
                AuthStatus.Content = "Offline";
                break;
            case 1:
                AuthStatus.Foreground = Brushes.Green;
                AuthStatus.Content = "Online";
                break;
        }
    }