Fixes random port testing problem

Fixes random port testing problem (#685).
Maybe it's possible to combine these try...except blocks.