当我尝试检查端口的可用性时收到错误代码 10061

I am getting error code 10061 when I try to check the availability of Port

我试图借助以下 C# 代码查找端口可用性,但出现异常 "Error Code 10061: Connection refused " 需要一些帮助来解决此问题。 打开的端口也显示为关闭,所以我遇到了这个问题

class Program
{
    private string _HostURI;
    private static bool m_blnIsWinAuth = false;

    static void Main(string[] args)
    {
        Hostname tcp;
        bool udp = false;
        Program pr = new Program();
        tcp = pr.GetPortAvability("127.0.0.1", 80, true);
        Console.WriteLine(tcp);
        Console.ReadLine();
    }

    public Hostname GetPortAvability(string _HostURI, int _PortNumber, bool isTCP)
    {
        Hostname returnValue = new Hostname();
        returnValue.HostName = _HostURI;
        returnValue.Result = false;

        IPAddress ipa = (IPAddress)Dns.GetHostAddresses(_HostURI)[0];

        try
        {
            Socket sock;
             if (isTCP)
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            else
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            sock.Connect(ipa, _PortNumber);

            if (sock.Connected)
                returnValue.Result = true;
            else
                returnValue.Result = false;

            sock.Close();
        }
        catch (SocketException se)
        {
            if (se.ErrorCode == 10061)
                returnValue.Result = true;
            else
                returnValue.Result = false;
        }
        catch (Exception e)
        {
            returnValue.Result = false;
        }

        return returnValue;       
    }
}

错误 10061 是服务器拒绝连接
这对我来说很有意义,因为您正在尝试连接到具有给定 IP 地址和端口号的套接字。但是没有其他套接字在等待连接。
你需要找到不同的方法。寻找 'endpoint'.