IbisSocket connect(TcpSendPort sp, ibis.ipl.impl.ReceivePortIdentifier rip,
            int timeout, boolean fillTimeout) throws IOException {
        IbisIdentifier id = (IbisIdentifier) rip.ibisIdentifier();
        String name = rip.name();
        IbisSocketAddress idAddr;
        synchronized (addresses) {
            idAddr = addresses.get(id);
            if (idAddr == null) {
                idAddr = new IbisSocketAddress(id.getImplementationData());
                addresses.put(id, idAddr);
            }
        }
        long startTime = System.currentTimeMillis();
        if (logger.isDebugEnabled()) {
            logger.debug("--> Creating socket for connection to " + name
                    + " at " + idAddr);
        }
        PortType sendPortType = sp.getPortType();
        do {
            DataOutputStream out = null;
            IbisSocket s = null;
            int result = -1;
            sp.printManagementProperties(System.out);
            try {
                s = factory.createClientSocket(idAddr, timeout, fillTimeout,
                        sp.managementProperties());
                s.setTcpNoDelay(true);
                out = new DataOutputStream(new BufferedArrayOutputStream(
                        s.getOutputStream()));
                out.writeUTF(name);
                sp.getIdent().writeTo(out);
                sendPortType.writeTo(out);
                out.flush();
                result = s.getInputStream().read();
                switch (result) {
                case ReceivePort.ACCEPTED:
                    return s;
                case ReceivePort.ALREADY_CONNECTED:
                    throw new AlreadyConnectedException("Already connected",
                            rip);
                case ReceivePort.TYPE_MISMATCH:
                    // Read receiveport type from input, to produce a
                    // better error message.
                    DataInputStream in = new DataInputStream(s.getInputStream());
                    PortType rtp = new PortType(in);
                    CapabilitySet s1 = rtp.unmatchedCapabilities(sendPortType);
                    CapabilitySet s2 = sendPortType.unmatchedCapabilities(rtp);
                    String message = "";
                    if (s1.size() != 0) {
                        message = message
                                + "\nUnmatched receiveport capabilities: "
                                + s1.toString() + ".";
                    }
                    if (s2.size() != 0) {
                        message = message
                                + "\nUnmatched sendport capabilities: "
                                + s2.toString() + ".";
                    }
                    throw new PortMismatchException(
                            "Cannot connect ports of different port types."
                                    + message, rip);
                case ReceivePort.DENIED:
                    throw new ConnectionRefusedException(
                            "Receiver denied connection", rip);
                case ReceivePort.NO_MANY_TO_X:
                    throw new ConnectionRefusedException(
                            "Receiver already has a connection and neither ManyToOne not ManyToMany "
                                    + "is set", rip);
                case ReceivePort.NOT_PRESENT:
                case ReceivePort.DISABLED:
                    // and try again if we did not reach the timeout...
                    if (timeout > 0
                            && System.currentTimeMillis() > startTime + timeout) {
                        throw new ConnectionTimedOutException(
                                "Could not connect", rip);
                    }
                    break;
                case -1:
                    throw new IOException("Encountered EOF in TcpIbis.connect");
                default:
                    throw new IOException("Illegal opcode in TcpIbis.connect");
                }
            } catch (SocketTimeoutException e) {
                throw new ConnectionTimedOutException("Could not connect", rip);
            } finally {
                if (result != ReceivePort.ACCEPTED) {
                    try {
                        if (out != null) {
                            out.close();
                        }
                    } catch (Throwable e) {
                        // ignored
                    }
                    try {
                        s.close();
                    } catch (Throwable e) {
                        // ignored
                    }
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // ignore
            }
        } while (true);
    }
IbisSocket connect(TcpSendPort sp, ibis.ipl.impl.ReceivePortIdentifier rip,
            int timeout, boolean fillTimeout) throws IOException {
        IbisIdentifier id = (IbisIdentifier) rip.ibisIdentifier();
        String name = rip.name();
        IbisSocketAddress idAddr;
        synchronized (addresses) {
            idAddr = addresses.get(id);
            if (idAddr == null) {
                idAddr = new IbisSocketAddress(id.getImplementationData());
                addresses.put(id, idAddr);
            }
        }
        long startTime = System.currentTimeMillis();
        if (logger.isDebugEnabled()) {
            logger.debug("--> Creating socket for connection to " + name
                    + " at " + idAddr);
        }
        PortType sendPortType = sp.getPortType();
        do {
            DataOutputStream out = null;
            IbisSocket s = null;
            int result = -1;
            sp.printManagementProperties(System.out);
            try {
                s = factory.createClientSocket(idAddr, timeout, fillTimeout,
                        sp.managementProperties());
                s.setTcpNoDelay(true);
                out = new DataOutputStream(new BufferedArrayOutputStream(
                        s.getOutputStream()));
                out.writeUTF(name);
                sp.getIdent().writeTo(out);
                sendPortType.writeTo(out);
                out.flush();
                result = s.getInputStream().read();
                switch (result) {
                case ReceivePort.ACCEPTED:
                    return s;
                case ReceivePort.ALREADY_CONNECTED:
                    throw new AlreadyConnectedException("Already connected",
                            rip);
                case ReceivePort.TYPE_MISMATCH:
                   
                   
                    DataInputStream in = new DataInputStream(s.getInputStream());
                    PortType rtp = new PortType(in);
                    CapabilitySet s1 = rtp.unmatchedCapabilities(sendPortType);
                    CapabilitySet s2 = sendPortType.unmatchedCapabilities(rtp);
                    String message = "";
                    if (s1.size() != 0) {
                        message = message
                                + "\nUnmatched receiveport capabilities: "
                                + s1.toString() + ".";
                    }
                    if (s2.size() != 0) {
                        message = message
                                + "\nUnmatched sendport capabilities: "
                                + s2.toString() + ".";
                    }
                    throw new PortMismatchException(
                            "Cannot connect ports of different port types."
                                    + message, rip);
                case ReceivePort.DENIED:
                    throw new ConnectionRefusedException(
                            "Receiver denied connection", rip);
                case ReceivePort.NO_MANY_TO_X:
                    throw new ConnectionRefusedException(
                            "Receiver already has a connection and neither ManyToOne not ManyToMany "
                                    + "is set", rip);
                case ReceivePort.NOT_PRESENT:
                case ReceivePort.DISABLED:
                   
                    if (timeout > 0
                            && System.currentTimeMillis() > startTime + timeout) {
                        throw new ConnectionTimedOutException(
                                "Could not connect", rip);
                    }
                    break;
                case -1:
                    throw new IOException("Encountered EOF in TcpIbis.connect");
                default:
                    throw new IOException("Illegal opcode in TcpIbis.connect");
                }
            } catch (SocketTimeoutException e) {
                throw new ConnectionTimedOutException("Could not connect", rip);
            } finally {
                if (result != ReceivePort.ACCEPTED) {
                    try {
                        if (out != null) {
                            out.close();
                        }
                    } catch (Throwable e) {
                       
                    }
                    try {
                        s.close();
                    } catch (Throwable e) {
                       
                    }
                }
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
               
            }
        } while (true);
    }
ibissocket connect(tcpsendport sp, ibis.ipl.impl.receiveportidentifier rip, int timeout, boolean filltimeout) throws ioexception { ibisidentifier id = (ibisidentifier) rip.ibisidentifier(); string name = rip.name(); ibissocketaddress idaddr; synchronized (addresses) { idaddr = addresses.get(id); if (idaddr == null) { idaddr = new ibissocketaddress(id.getimplementationdata()); addresses.put(id, idaddr); } } long starttime = system.currenttimemillis(); if (logger.isdebugenabled()) { logger.debug("--> creating socket for connection to " + name + " at " + idaddr); } porttype sendporttype = sp.getporttype(); do { dataoutputstream out = null; ibissocket s = null; int result = -1; sp.printmanagementproperties(system.out); try { s = factory.createclientsocket(idaddr, timeout, filltimeout, sp.managementproperties()); s.settcpnodelay(true); out = new dataoutputstream(new bufferedarrayoutputstream( s.getoutputstream())); out.writeutf(name); sp.getident().writeto(out); sendporttype.writeto(out); out.flush(); result = s.getinputstream().read(); switch (result) { case receiveport.accepted: return s; case receiveport.already_connected: throw new alreadyconnectedexception("already connected", rip); case receiveport.type_mismatch: datainputstream in = new datainputstream(s.getinputstream()); porttype rtp = new porttype(in); capabilityset s1 = rtp.unmatchedcapabilities(sendporttype); capabilityset s2 = sendporttype.unmatchedcapabilities(rtp); string message = ""; if (s1.size() != 0) { message = message + "\nunmatched receiveport capabilities: " + s1.tostring() + "."; } if (s2.size() != 0) { message = message + "\nunmatched sendport capabilities: " + s2.tostring() + "."; } throw new portmismatchexception( "cannot connect ports of different port types." + message, rip); case receiveport.denied: throw new connectionrefusedexception( "receiver denied connection", rip); case receiveport.no_many_to_x: throw new connectionrefusedexception( "receiver already has a connection and neither manytoone not manytomany " + "is set", rip); case receiveport.not_present: case receiveport.disabled: if (timeout > 0 && system.currenttimemillis() > starttime + timeout) { throw new connectiontimedoutexception( "could not connect", rip); } break; case -1: throw new ioexception("encountered eof in tcpibis.connect"); default: throw new ioexception("illegal opcode in tcpibis.connect"); } } catch (sockettimeoutexception e) { throw new connectiontimedoutexception("could not connect", rip); } finally { if (result != receiveport.accepted) { try { if (out != null) { out.close(); } } catch (throwable e) { } try { s.close(); } catch (throwable e) { } } } try { thread.sleep(100); } catch (interruptedexception e) { } } while (true); }