Changed port checking logic to fix crash