I apologize for the late response again. You probably already figured this out by now but
You could also do builder -> AddListeningPort("address:0", creds, &selected_port)
(i.e pass port number as "0" when you pass the address to AddListeningPort API) and this picks an available port and returns via selected_port
(The only issue with this is that you will then have to figure out a way to let your client know what the server port is. This is why, we use a 'port server' utility in our unit tests that I mentioned earlier..Yes, It does have the issue you mentioned in theory- i.e the port might be used by some other process between the port check and BuildAndStart)
-Sree