refatoring: extract get_port and init_socket