fix attempt for issue #3495 by checking and handling occupied ports