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

namespace NATS.Server.Benchmark.Tests.Infrastructure;

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