reading/writing to socket + error handling