tunnel (Just host) = case (splitHostAndPort (B.unpack host)) of
                        Just (host,port) -> flip responseRaw (tunnel Nothing) $ \clientBody response -> do
                                                runTCPClient (clientSettingsTCP port (B.pack host)) $ \remoteData -> do
                                                    response "HTTP/1.1 200 Connection Established\r\nProxy-agent: paranoia\r\n\r\n"
                                                    connClosed <- newEmptyMVar
                                                    reader <- forkThread $ foreverUntil (isEmptyMVar connClosed) (do
                                                                            inp <- clientBody
                                                                            if (not $ B.null inp) then appWrite remoteData inp else usleep 100000
                                                                            )
                                                    doUntil (isAppDataSocketConnected remoteData) (do 
                                                                                            d <- appRead remoteData 
                                                                                            if (B.null d) then usleep 100000 else response d) `finally` putMVar connClosed ()
                                                    takeMVar reader
                        Nothing -> (tunnel Nothing)
tunnel (Just host) = case (splitHostAndPort (B.unpack host)) of
                        Just (host,port) -> flip responseRaw (tunnel Nothing) $ \clientBody response -> do
                                                runTCPClient (clientSettingsTCP port (B.pack host)) $ \remoteData -> do
                                                    response "HTTP/1.1 200 Connection Established\r\nProxy-agent: paranoia\r\n\r\n"
                                                    connClosed <- newEmptyMVar
                                                    reader <- forkThread $ foreverUntil (isEmptyMVar connClosed) (do
                                                                            inp <- clientBody
                                                                            if (not $ B.null inp) then appWrite remoteData inp else usleep 100000
                                                                            )
                                                    doUntil (isAppDataSocketConnected remoteData) (do 
                                                                                            d <- appRead remoteData 
                                                                                            if (B.null d) then usleep 100000 else response d) `finally` putMVar connClosed ()
                                                    takeMVar reader
                        Nothing -> (tunnel Nothing)
tunnel (Just host) = case (splitHostAndPort (B.unpack host)) of
                        Just (host,port) -> flip responseRaw (tunnel Nothing) $ \clientBody response -> do
                                                runTCPClient (clientSettingsTCP port (B.pack host)) $ \remoteData -> do
                                                    response "HTTP/1.1 200 Connection Established\r\nProxy-agent: paranoia\r\n\r\n"
                                                    connClosed <- newEmptyMVar
                                                    reader <- forkThread $ foreverUntil (isEmptyMVar connClosed) (do
                                                                            inp <- clientBody
                                                                            if (not $ B.null inp) then appWrite remoteData inp else usleep 100000
                                                                            )
                                                    doUntil (isAppDataSocketConnected remoteData) (do 
                                                                                            d <- appRead remoteData 
                                                                                            if (B.null d) then usleep 100000 else response d) `finally` putMVar connClosed ()
                                                    takeMVar reader
                        Nothing -> (tunnel Nothing)