move socket writing logic into Connection.write()