more explicit socket error
change code structure
fix some output but
restructure code