final version with port_num revised