refactor code

use open_socket() function to unify all socket connect/bind code