private RestResponse connect(String[][] headers,
            InputStream data) throws IOException
    {
        try
        {
            // Send data
            setHeaders(headers);
            
            String method = conn.getRequestMethod();
            
            byte[] buffer = new byte[1024];
            int count = 0;
            
            if (method.equals("PUT") || method.equals("POST"))
            {
                if (data != null)
                {
                    conn.setDoOutput(true);
                    OutputStream os = conn.getOutputStream();
                    
                    while ((count = data.read(buffer)) != -1)
                    {
                        os.write(buffer, 0, count);
                    }
                    os.flush();
                }
            }
            
            RestResponse response = new RestResponse();
            InputStream is = conn.getInputStream();
            
            while ((count = is.read(buffer)) != -1)
            {
                response.write(buffer, 0, count);
            }
            
            response.setResponseCode(conn.getResponseCode());
            response.setResponseMessage(conn.getResponseMessage());
            response.setContentType(conn.getContentType());
            response.setContentEncoding(conn.getContentEncoding());
            response.setLastModified(conn.getLastModified());
            
            return response;
        }
        catch (Exception e)
        {
            String errMsg = "Cannot connect to :" + conn.getURL();
            try
            {
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
                String line;
                StringBuffer buf = new StringBuffer();
                while ((line = rd.readLine()) != null)
                {
                    buf.append(line);
                    buf.append('\n');
                }
                errMsg = buf.toString();
            }
            finally
            {
                throw new IOException(errMsg);
            }
        }
    }