use random port for tests

if port 9999 is in use the tests will fail because the port is hardcoded
I have added a try/catch with Random. It fixes: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=755320