Separation of error handling and logic

  • I divided the try-catch and logic
  • changed it to an intuitive code through the ERROR_INVALID_PORT constant.

Comment From: snicoll

Duplicate of #26706