sendAssociationACTCP(PRIVATE_NETWORKKEY ** /*network*/,
                     DUL_ASSOCIATESERVICEPARAMETERS * params,
                     PRIVATE_ASSOCIATIONKEY ** association)
{
    PRV_ASSOCIATEPDU
    associateReply;
    unsigned char
        buffer[4096],
       *b;
    unsigned long length = 0;
    int nbytes;
    DUL_ASSOCIATESERVICEPARAMETERS localService;

    OFBitmanipTemplate<char>::zeroMem((char *)&associateReply, sizeof(PRV_ASSOCIATEPDU)); // initialize PDU
    // associateReply.presentationContextList = NULL;

    localService = *params;
    OFCondition cond = constructAssociatePDU(&localService, DUL_TYPEASSOCIATEAC,
                                 &associateReply);
    if (cond.bad())
    {
        DCMNET_ERROR(cond.text());
        return cond;
    }

    // we need to have length+6 bytes in buffer, but 4 bytes reserve won't hurt
    if (associateReply.length + 10 <= sizeof(buffer)) b = buffer;
    else {
        b = (unsigned char*)malloc(size_t(associateReply.length + 10));
        if (b == NULL)  return EC_MemoryExhausted;
    }
    cond = streamAssociatePDU(&associateReply, b,
                              associateReply.length + 10, &length);

    if ((*association)->associatePDUFlag)
    {
      // copy A-ASSOCIATE-AC PDU
      (*association)->associatePDU = new char[length];
      if ((*association)->associatePDU)
      {
        memcpy((*association)->associatePDU, b, (size_t) length);
        (*association)->associatePDULength = length;
      }
    }

    destroyPresentationContextList(&associateReply.presentationContextList);
    destroyUserInformationLists(&associateReply.userInfo);

    if (cond.bad()) return cond;

    do {
      nbytes = (*association)->connection ? (*association)->connection->write((char*)b, size_t(associateReply.length + 6)) : 0;
    } while (nbytes == -1 && OFStandard::getLastNetworkErrorCode().value() == DCMNET_EINTR);
    if ((unsigned long) nbytes != associateReply.length + 6)
    {
      OFString msg = "TCP I/O Error (";
      msg += OFStandard::getLastNetworkErrorCode().message();
      msg += ") occurred in routine: sendAssociationACTCP";
      return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
    }
    if (b != buffer) free(b);
    return EC_Normal;
}
sendAssociationACTCP(PRIVATE_NETWORKKEY **  ,
                     DUL_ASSOCIATESERVICEPARAMETERS * params,
                     PRIVATE_ASSOCIATIONKEY ** association)
{
    PRV_ASSOCIATEPDU
    associateReply;
    unsigned char
        buffer[4096],
       *b;
    unsigned long length = 0;
    int nbytes;
    DUL_ASSOCIATESERVICEPARAMETERS localService;

    OFBitmanipTemplate<char>::zeroMem((char *)&associateReply, sizeof(PRV_ASSOCIATEPDU));  
     

    localService = *params;
    OFCondition cond = constructAssociatePDU(&localService, DUL_TYPEASSOCIATEAC,
                                 &associateReply);
    if (cond.bad())
    {
        DCMNET_ERROR(cond.text());
        return cond;
    }

     
    if (associateReply.length + 10 <= sizeof(buffer)) b = buffer;
    else {
        b = (unsigned char*)malloc(size_t(associateReply.length + 10));
        if (b == NULL)  return EC_MemoryExhausted;
    }
    cond = streamAssociatePDU(&associateReply, b,
                              associateReply.length + 10, &length);

    if ((*association)->associatePDUFlag)
    {
       
      (*association)->associatePDU = new char[length];
      if ((*association)->associatePDU)
      {
        memcpy((*association)->associatePDU, b, (size_t) length);
        (*association)->associatePDULength = length;
      }
    }

    destroyPresentationContextList(&associateReply.presentationContextList);
    destroyUserInformationLists(&associateReply.userInfo);

    if (cond.bad()) return cond;

    do {
      nbytes = (*association)->connection ? (*association)->connection->write((char*)b, size_t(associateReply.length + 6)) : 0;
    } while (nbytes == -1 && OFStandard::getLastNetworkErrorCode().value() == DCMNET_EINTR);
    if ((unsigned long) nbytes != associateReply.length + 6)
    {
      OFString msg = "TCP I/O Error (";
      msg += OFStandard::getLastNetworkErrorCode().message();
      msg += ") occurred in routine: sendAssociationACTCP";
      return makeDcmnetCondition(DULC_TCPIOERROR, OF_error, msg.c_str());
    }
    if (b != buffer) free(b);
    return EC_Normal;
}