Horizon – Rev 12

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Horizon.Utilities.Networking
{
    public static class Miscellaneous
    {
        private static readonly IPEndPoint DefaultLoopbackEndpoint = new IPEndPoint(IPAddress.Loopback, port: 0);

        public static ushort GetAvailableTcpPort()
        {
            using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(DefaultLoopbackEndpoint);
            var port = ushort.Parse($"{((IPEndPoint)socket.LocalEndPoint).Port}");
            return port;
        }
    }
}