fixed some bugs

made socket code more intuitive.