refactory socket related code.