func sockReadMessage(buf *streambuf.Buffer, in io.Reader) (*message, error) { for { // try parse message from buffered data msg, err := readMessage(buf) if msg != nil || (err != nil && err != streambuf.ErrNoMoreBytes) { return msg, err } // read next bytes from socket if incomplete message in buffer buffer := make([]byte, 1024) n, err := in.Read(buffer) if err != nil { return nil, err } buf.Write(buffer[:n]) } }