Improve random port check.