packed_data = []
                old_ts = new_ts
            elif not packed_data:
                old_ts = new_ts

            packed_data.append(data)

finally:
    # Stop recording
    stop_recording(recording_id)
    print("Recording stopped")

    # Check recording status
    status = wait_for_status('/api/recordings/' + recording_id + '/status',
                             'rec_state', ['failed', 'done'])
    if status == 'failed':
        print('Recording failed')
    else:
        print('Recording successful')

    # Send end of recording
    data = "END"
    zmq_socket.send(msgpack.packb((data, mq.get_shifted_time())))

    # Close la conneccion
    zmq_socket.send(b"CLOSE")
    zmq_socket.close()
    print("DONE")

running = False
        settings['messaging']['mocap_host']
    ],
                    stdout=PIPE,
                    stderr=PIPE)
elif platform == 'win64':
    process = Popen([
        './vicon_windows64/ViconDataStreamSDK_CPPTest.exe',
        settings['messaging']['mocap_host']
    ],
                    stdout=PIPE,
                    stderr=PIPE)

print('[*] Serving at {}. To exit press enter'.format(zmq_server_addr))

# Send each data stream
try:
    frame = []
    for stdout_line in iter(process.stdout.readline, ""):
        #if DEBUG: print(stdout_line, "")
        frame.append(stdout_line)

        if stdout_line == 'Waiting for new frame...\n':
            if DEBUG: print(frame)
            zmq_socket.send(msgpack.packb((frame, mq.get_shifted_time())))
            frame = []

finally:
    # Close connection
    zmq_socket.send(b'CLOSE')
    zmq_socket.close()