<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def start_gen(port, ser='msgpack', version='2.2', detector='AGIPD', raw=False, nsources=1, datagen='random', *, debug=True):
 """"Karabo bridge server simulation. Simulate a Karabo Bridge server and send random data from a detector, either AGIPD or LPD. Parameters port: str The port to on which the server is bound. ser: str, optional The serialization algorithm, default is msgpack. version: str, optional The container version of the serialized data. detector: str, optional The data format to send, default is AGIPD detector. raw: bool, optional Generate raw data output if True, else CORRECTED. Default is False. nsources: int, optional Number of sources. datagen: string, optional Generator function used to generate detector data. Default is random. """

    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.setsockopt(zmq.LINGER, 0)
    socket.bind('tcp://*:{}'.format(port))

    if ser != 'msgpack':
        raise ValueError("Unknown serialisation format %s" % ser)
    serialize = partial(msgpack.dumps, use_bin_type=True)
    det = Detector.getDetector(detector, raw=raw, gen=datagen)
    generator = generate(det, nsources)

    print('Simulated Karabo-bridge server started on:\ntcp://{}:{}'.format(
          uname().nodename, port))

    t_prev = time()
    n = 0

    try:
        while True:
            msg = socket.recv()
            if msg == b'next':
                train = next(generator)
                msg = containize(train, ser, serialize, version)
                socket.send_multipart(msg, copy=False)
                if debug:
                    print('Server : emitted train:',
                          train[1][list(train[1].keys())[0]]['timestamp.tid'])
                n += 1
                if n % TIMING_INTERVAL == 0:
                    t_now = time()
                    print('Sent {} trains in {:.2f} seconds ({:.2f} Hz)'
                          ''.format(TIMING_INTERVAL, t_now - t_prev,
                                    TIMING_INTERVAL / (t_now - t_prev)))
                    t_prev = t_now
            else:
                print('wrong request')
                break
    except KeyboardInterrupt:
        print('\nStopped.')
    finally:
        socket.close()
        context.destroy()