      SUBROUTINE PLU.TCP.OPEN(HOST.IP,HOST.PORT,R.PLCB,R.ERR)
*PIC-LAN
*
*ALL
*PC
*N
*
*
*
*
******************************************************************************
******************************************************************************
***                                                                        ***
*** (C) Copyright 1990-2005 Doug Dumitru, All Rights Reserved.             ***
***                                                                        ***
*** This program is licensed under the terms of the GNU General Public     ***
*** License, version 2.0 with attached notices.                            ***
***                                                                        ***
*** The full text can be found in the LICENSE.TXT file in the              ***
*** PLIP.BP database file (the /usr/qmweb/PLIP.BP/PLIP.BP directory).      ***
***                                                                        ***
******************************************************************************
******************************************************************************
*
      INCLUDE SYS.TYPE.INCLUDE
*
      INCLUDE PL.COMMON
      INCLUDE PL.COMMON.DEFS
      INCLUDE PL.DEFS
      INCLUDE PLIP.COMMON
      INCLUDE PLW.COMMON
*
      PL_TCP_OPEN PLIP$MYIPS<1> , 0 , HOST.IP , HOST.PORT , PORT.NO ELSE
         R.ERR = 'Error during PL_TCP_OPEN'
         RETURN
      END
*
      R.PLCB = RES
*
      T0 = PL_NOW()
      LOOP
         PL_PLCB.STATUS R.PLCB ELSE
            PRINT 'Error getting TCP connection status'
            PL_DEALLOC.PLCB R.PLCB ELSE NULL
            RETURN
         END
         ST = PL_TCP.STATUS_STATE()
         IF ST <= 1 OR ST > 5 THEN
            R.ERR = 'TCP connection closed'
            PL_DEALLOC.PLCB R.PLCB ELSE NULL
            RETURN
         END
         TM = PL_NOW() - T0
         IF TM > 30 THEN
            PRINT 'Timeout error opening TCP connection'
            PL_DEALLOC.PLCB R.PLCB ELSE NULL
            RETURN
         END
      UNTIL ST = 5 DO
         IF TM >= 2 THEN
            SLEEP 1
         END ELSE
            PL_RQM
         END
      REPEAT
*
      R.ERR = ''
*
      RETURN
   END
