public void connect() throws IOException
	{
		try
		{
			sslsocket = (SSLSocket)SSLSocketFactory.getDefault().createSocket(server, port);
			in = new BufferedInputStream(sslsocket.getInputStream());
			out = new DataOutputStream(sslsocket.getOutputStream());

			doHandshake();
		}
		catch (IOException e)
		{
			// If we failed to set up the socket, assume it's because we needed a certificate
			getCertificate();
			// And use the certificate
			openSocketWithCert();

			// And try to handshake again
			in = new BufferedInputStream(sslsocket.getInputStream());
			out = new DataOutputStream(sslsocket.getOutputStream());

			doHandshake();
		}

		// Start reading responses
		pr = new RTMPPacketReader(in);

		// Handle preconnect Messages?
		// -- 02 | 00 00 00 | 00 00 05 | 06 00 00 00 00 | 00 03 D0 90 02

		// Connect
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("app", app);
		params.put("flashVer", "WIN 10,1,85,3");
		params.put("swfUrl", swfUrl);
		params.put("tcUrl", "rtmps://" + server + ":" + port);
		params.put("fpad", false);
		params.put("capabilities", 239);
		params.put("audioCodecs", 3191);
		params.put("videoCodecs", 252);
		params.put("videoFunction", 1);
		params.put("pageUrl", pageUrl);
		params.put("objectEncoding", 3);

		byte[] connect = aec.encodeConnect(params);

		out.write(connect, 0, connect.length);
		out.flush();

		while (!results.containsKey(1))
			sleep(10);
		TypedObject result = results.get(1);
		DSId = result.getTO("data").getString("id");

		connected = true;
	}