findAvailablePort function
Returns the first available port on host starting from startPort.
Continues incrementing the port number until a free socket is found.
Implementation
Future<int> findAvailablePort(String host, int startPort) async {
int candidate = startPort;
while (true) {
if (await isPortAvailable(host, candidate)) {
return candidate;
}
candidate++;
}
}