'Internal legacy write helper.'

def _legacy_write(self, sock_info, name, cmd, acknowledged, op_id, bypass_doc_val, func, *args):

    if (bypass_doc_val and (not acknowledged) and (sock_info.max_wire_version >= 4)):
        raise OperationFailure('Cannot    set    bypass_document_validation    with    unacknowledged    write    concern')
    listeners = self.database.client._event_listeners
    publish = listeners.enabled_for_commands
    if publish:
        start = datetime.datetime.now()
    (rqst_id, msg, max_size) = func(*args)
    if publish:
        duration = (datetime.datetime.now() - start)
        listeners.publish_command_start(cmd, self.__database.name, rqst_id, sock_info.address, op_id)
        start = datetime.datetime.now()
    try:
        result = sock_info.legacy_write(rqst_id, msg, max_size, acknowledged)
    except Exception as exc:
        if publish:
            dur = ((datetime.datetime.now() - start) + duration)
            if isinstance(exc, OperationFailure):
                details = exc.details
                if (details.get('ok') and ('n' in details)):
                    reply = message._convert_write_result(name, cmd, details)
                    listeners.publish_command_success(dur, reply, name, rqst_id, sock_info.address, op_id)
                    raise
            else:
                details = message._convert_exception(exc)
            listeners.publish_command_failure(dur, details, name, rqst_id, sock_info.address, op_id)
        raise
    if publish:
        if (result is not None):
            reply = message._convert_write_result(name, cmd, result)
        else:
            reply = {'ok': 1}
        duration = ((datetime.datetime.now() - start) + duration)
        listeners.publish_command_success(duration, reply, name, rqst_id, sock_info.address, op_id)
    return result
