try { socket.close(); } catch (IOException e) {}
try { if (socket != null) socket.close(); } catch (IOException e) {}
    public void connect(String host, int port, SocketFactory socketFactory, String subdomain)            throws ComponentException {        try {            // Open a socket to the server            this.socket = socketFactory.createSocket(host, port);            // Assume that the host name matches the real name of the XMPP server            this.domain = subdomain + "." + host;            this.subdomain = subdomain;            // Keep these variables that will be used in case a reconnection is required            this.host= host;            this.port = port;            this.socketFactory = socketFactory;            try {                factory = XmlPullParserFactory.newInstance();                reader = new XPPPacketReader();                reader.setXPPFactory(factory);                reader.getXPPParser().setInput(new InputStreamReader(socket.getInputStream(),                        CHARSET));                // Get a writer for sending the open stream tag                writer =                        new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),                                CHARSET));                // Open the stream.                StringBuilder stream = new StringBuilder();                stream.append("<stream:stream");                stream.append(" xmlns=\"jabber:component:accept\"");                stream.append(" xmlns:stream=\"http://etherx.jabber.org/streams\"");                stream.append(" to=\"" + domain + "\">");                writer.write(stream.toString());                writer.flush();                stream = null;                // Get the answer from the server                XmlPullParser xpp = reader.getXPPParser();                for (int eventType = xpp.getEventType(); eventType != XmlPullParser.START_TAG;) {                    eventType = xpp.next();                }                // Set the streamID returned from the server                connectionID = xpp.getAttributeValue("", "id");                if (xpp.getAttributeValue("", "from") != null) {                    this.domain = xpp.getAttributeValue("", "from");                }                xmlSerializer = new XMLWriter(writer);                // Handshake with the server                stream = new StringBuilder();                stream.append("<handshake>");                stream.append(StringUtils.hash(connectionID + manager.getSecretKey(subdomain)));                stream.append("</handshake>");                writer.write(stream.toString());                writer.flush();                stream = null;                // Get the answer from the server                try {                    Element doc = reader.parseDocument().getRootElement();                    if ("error".equals(doc.getName())) {                        StreamError error = new StreamError(doc);                        // Close the connection                        socket.close();                        socket = null;                        // throw the exception with the wrapped error                        throw new ComponentException(error);                    }                    // Everything went fine                } catch (DocumentException e) {                    try { socket.close(); } catch (IOException ioe) {}                    throw new ComponentException(e);                } catch (XmlPullParserException e) {                    try { socket.close(); } catch (IOException ioe) {}                    throw new ComponentException(e);                }            } catch (XmlPullParserException e) {                try { socket.close(); } catch (IOException ioe) {}                throw new ComponentException(e);            }        }        catch (UnknownHostException uhe) {            try { socket.close(); } catch (IOException e) {}            throw new ComponentException(uhe);        }        catch (IOException ioe) {            try { if (socket != null) socket.close(); } catch (IOException e) {}            throw new ComponentException(ioe);        }    }