using System.Net;
using System.Net.Sockets;

namespace NATS.Server.TestUtilities;

public static class TestPortAllocator
{
    public static int GetFreePort()
    {
        using var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        sock.Bind(new IPEndPoint(IPAddress.Loopback, 0));
        return ((IPEndPoint)sock.LocalEndPoint!).Port;
    }
}
