# Buggy code
def send(self, frame, flags=0, copy=True, track=False):
    """Send a single frame while enforcing VIP protocol.

    Expects frames to be sent in the following order:

       PEER USER_ID MESSAGE_ID SUBSYSTEM [ARG]...

    If the socket is a ROUTER, an INTERMEDIARY must be sent before
    PEER. The VIP protocol signature, PROTO, is automatically sent
    between PEER and USER_ID. Zero or more ARG frames may be sent
    after SUBSYSTEM, which may not be empty. All frames up to
    SUBSYSTEM must be sent with the SNDMORE flag.
    """
    self.wait_send(flags)
    state = self._send_state
    if state == 4:
        # Verify that subsystem has some non-space content
        subsystem = bytes(frame)
        if not subsystem.strip():
            raise ProtocolError("invalid subsystem: {!r}".format(subsystem))
    if not flags & SNDMORE:
        # Must have SNDMORE flag until sending SUBSYSTEM frame.
        if state < 4:
            raise ProtocolError(
                "expecting at least {} more frames".format(4 - state - 1)
            )
        # Reset the send state when the last frame is sent
        self._send_state = -1 if self.type == ROUTER else 0
    elif state < 5:
        if state == 1:
            # Automatically send PROTO frame
            super(_Socket, self).send(b"VIP1", flags=flags | SNDMORE)
            state += 1
        self._send_state = state + 1
    try:
        super(_Socket, self).send(frame, flags=flags, copy=copy, track=track)
    except Exception:
        self._send_state = state
        raise


# Traceback (ProtocolError)
Traceback (most recent call last):
File "/home/kyle/workspaces/volttron/env/local/lib/python2.7/site-packages/gevent/greenlet.py", line 327, in run
result = self._run(*self.args, **self.kwargs)
File "/home/kyle/workspaces/volttron/volttron/platform/vip/agent/subsystems/pubsub.py", line 280, in subscribe
self.rpc().call(peer, 'pubsub.subscribe', prefix, bus=bus).get()
File "/home/kyle/workspaces/volttron/volttron/platform/vip/agent/subsystems/rpc.py", line 282, in call
self.core().socket.send_vip(peer, 'RPC', [request], msg_id=ident)
File "/home/kyle/workspaces/volttron/volttron/platform/vip/socket.py", line 225, in send_vip
raise ProtocolError('previous send operation is not complete')
ProtocolError: previous send operation is not complete
<Greenlet at 0xb2fc002cL: subscribe(<volttron.platform.vip.agent.subsystems.pubsub.Pub, peer='pubsub', callback=<bound method ActuatorAgent.handle_set of <__main_, prefix=Topic(u'devices/actuators/set'))> failed with ProtocolError

Traceback (most recent call last):
File "/home/kyle/workspaces/volttron/env/local/lib/python2.7/site-packages/gevent/greenlet.py", line 327, in run
result = self._run(*self.args, **self.kwargs)
File "/home/kyle/workspaces/volttron/volttron/platform/vip/agent/subsystems/pubsub.py", line 280, in subscribe
self.rpc().call(peer, 'pubsub.subscribe', prefix, bus=bus).get()
File "/home/kyle/workspaces/volttron/volttron/platform/vip/agent/subsystems/rpc.py", line 282, in call
self.core().socket.send_vip(peer, 'RPC', [request], msg_id=ident)
File "/home/kyle/workspaces/volttron/volttron/platform/vip/socket.py", line 225, in send_vip
raise ProtocolError('previous send operation is not complete')
ProtocolError: previous send operation is not complete
<Greenlet at 0xb2fc007cL: subscribe(<volttron.platform.vip.agent.subsystems.pubsub.Pub, peer='pubsub', callback=<bound method ActuatorAgent.handle_schedule_reques, prefix=Topic(u'devices/actuators/schedule/request'))> failed with ProtocolError

# Fixed code
def send(self, frame, flags=0, copy=True, track=False):
    """Send a single frame while enforcing VIP protocol.

    Expects frames to be sent in the following order:

       PEER USER_ID MESSAGE_ID SUBSYSTEM [ARG]...

    If the socket is a ROUTER, an INTERMEDIARY must be sent before
    PEER. The VIP protocol signature, PROTO, is automatically sent
    between PEER and USER_ID. Zero or more ARG frames may be sent
    after SUBSYSTEM, which may not be empty. All frames up to
    SUBSYSTEM must be sent with the SNDMORE flag.
    """
    with self._sending(flags) as flags:
        state = self._send_state
        if state == 4:
            # Verify that subsystem has some non-space content
            subsystem = bytes(frame)
            if not subsystem.strip():
                raise ProtocolError("invalid subsystem: %s" % subsystem)
        if not flags & SNDMORE:
            # Must have SNDMORE flag until sending SUBSYSTEM frame.
            if state < 4:
                raise ProtocolError(
                    "expecting at least %d more frames" % (4 - state - 1)
                )
            # Reset the send state when the last frame is sent
            self._send_state = -1 if self.type == ROUTER else 0
        elif state < 5:
            if state == 1:
                # Automatically send PROTO frame
                super(_Socket, self).send(b"VIP1", flags=flags | SNDMORE)
                state += 1
            self._send_state = state + 1
        try:
            super(_Socket, self).send(frame, flags=flags, copy=copy, track=track)
        except Exception:
            self._send_state = state
            raise