Get available port from specified range
There's an obvious flaw if the candidate port is closed on 127.0.0.1 but open on a different interface that you want your service to bind to.  That's particularly relevant if you want it to listen on all interfaces (0.0.0.0).

If your service binds to the localhost address, then that's obviously not a concern.