improved network socket logic