/** Constructor declaration */
  public JICPConnection(TransportAddress ta, int timeout) throws IOException {
    // #MIDP_EXCLUDE_BEGIN
    // For some reason the local address or port may be in use
    while (true) {
      try {
        // #PJAVA_EXCLUDE_BEGIN
        sc = new Socket();
        bindSocket(sc);
        sc.setTcpNoDelay(true);
        sc.connect(new InetSocketAddress(ta.getHost(), Integer.parseInt(ta.getPort())), timeout);
        // #PJAVA_EXCLUDE_END
        /*#PJAVA_INCLUDE_BEGIN
        sc = new Socket(ta.getHost(), Integer.parseInt(ta.getPort()));
        #PJAVA_INCLUDE_END*/
        is = sc.getInputStream();
        os = getOutputStream();
        break;
      } catch (BindException be) {
        // Do nothing and try again
      }
    }
    // #MIDP_EXCLUDE_END

    /*#MIDP_INCLUDE_BEGIN
    String url = "socket://"+ta.getHost()+":"+ta.getPort();
    sc = (StreamConnection) Connector.open(url, Connector.READ_WRITE, false);
    is = sc.openInputStream();
    os = getOutputStream();
    #MIDP_INCLUDE_END*/
  }
 public void close() throws IOException {
   IOException firstExc = null;
   if (is != null) {
     try {
       is.close();
     } catch (IOException e) {
       firstExc = e;
     }
     is = null;
   }
   if (os != null) {
     try {
       os.close();
     } catch (IOException e) {
       firstExc = (firstExc != null ? firstExc : e);
     }
     os = null;
   }
   if (sc != null) {
     try {
       sc.close();
     } catch (IOException e) {
       firstExc = (firstExc != null ? firstExc : e);
     }
     sc = null;
   }
   if (firstExc != null) {
     throw firstExc;
   }
 }
 public JICPPacket readPacket() throws IOException {
   if (sc != null) {
     if (is == null) {
       // #MIDP_EXCLUDE_BEGIN
       is = sc.getInputStream();
       // #MIDP_EXCLUDE_END
       /*#MIDP_INCLUDE_BEGIN
       is = sc.openInputStream();
       #MIDP_INCLUDE_END*/
     }
     return JICPPacket.readFrom(is);
   } else {
     throw new IOException("Connection closed");
   }
 }
 // #MIDP_EXCLUDE_BEGIN
 public void setReadTimeout(int timeout) throws IOException {
   if (sc != null) {
     sc.setSoTimeout(timeout);
   }
 }
 public int getLocalPort() {
   return sc.getLocalPort();
 }
 public String getLocalHost() {
   return sc.getLocalAddress().getHostAddress();
 }
 public String getRemoteHost() throws Exception {
   return sc.getInetAddress().getHostAddress();
 }