'Parses for messages in the buffer *buf*.  It is assumed that
the buffer contains the start character for a message, but that it
may contain only part of the rest of the message.
Returns an array of messages, and the buffer remainder that
didn\'t contain any full messages.'

def _parse_messages(self):

    msgs = []
    end_idx = 0
    buf = self._buf
    while buf:
        if (self.version in ['7', '8', '13']):
            frame = self.decode_hybi(buf, base64=False)
            if (frame['payload'] == None):
                break
            elif (frame['opcode'] == 8):
                self.websocket_closed = True
                break
            else:
                msgs.append(frame['payload'])
                if frame['left']:
                    buf = buf[(- frame['left']):]
                else:
                    buf = ''
        else:
            frame_type = ord(buf[0])
            if (frame_type == 0):
                end_idx = buf.find('\xff')
                if (end_idx == (-1)):
                    break
                msgs.append(buf[1:end_idx].decode('utf-8', 'replace'))
                buf = buf[(end_idx + 1):]
            elif (frame_type == 255):
                assert (ord(buf[1]) == 0), ('Unexpected    closing    handshake:    %r' % buf)
                self.websocket_closed = True
                break
            else:
                raise ValueError(("Don't    understand    how    to    parse    this    type    of    message:    %r" % buf))
    self._buf = buf
    return msgs
