Opening TCP listening ports refactored into a function.