Fixes random port testing problem

Fixes random port testing problem (#685).
It was combined actually, but I thought it would be better to see which part has error so that we will know we are doing something wrong or not (for example, I made a mistake with setsockopt(), it was failing all the time and I didn't know what was wrong).