open xs = let
    cont (DispName p h d s)
        | null h || null p && h == "unix" = openUnix p
             ("/tmp/.X11-unix/X" ++ show d)
        | otherwise = openTCP p h (6000 + d)

    openTCP proto host port
        | proto == [] || proto == "tcp" =
            let addrInfo = defaultHints { addrFlags  = [ AI_ADDRCONFIG
                                                       , AI_NUMERICSERV
                                                       ]
                                        , addrFamily = AF_UNSPEC
                                        , addrSocketType = Stream
                                        }
                conn (AddrInfo _ fam socktype proto addr _) Nothing = do
                   fd <- socket fam socktype proto
                   connect fd addr
                   return $ Just fd
                conn _ x = return x
            in getAddrInfo (Just addrInfo) (Just host) (Just (show port))
               >>= foldrM conn Nothing
        | otherwise = error "'protocol' should be empty or 'tcp'"
 
    in case parseDisplay xs of
        (Left e) -> error (show e)
        (Right x) -> do
             socket <- cont x >>= return . fromMaybe
                 (error "couldn't open socket")
             auth <- getAuthInfo socket (display x) 
             hndl <- socketToHandle socket ReadWriteMode
             return (hndl, auth, x)
open xs = let
    cont (DispName p h d s)
        | null h || null p && h == "unix" = openUnix p
             ("/tmp/.X11-unix/X" ++ show d)
        | otherwise = openTCP p h (6000 + d)

    openTCP proto host port
        | proto == [] || proto == "tcp" =
            let addrInfo = defaultHints { addrFlags  = [ AI_ADDRCONFIG
                                                       , AI_NUMERICSERV
                                                       ]
                                        , addrFamily = AF_UNSPEC
                                        , addrSocketType = Stream
                                        }
                conn (AddrInfo _ fam socktype proto addr _) Nothing = do
                   fd <- socket fam socktype proto
                   connect fd addr
                   return $ Just fd
                conn _ x = return x
            in getAddrInfo (Just addrInfo) (Just host) (Just (show port))
               >>= foldrM conn Nothing
        | otherwise = error "'protocol' should be empty or 'tcp'"
 
    in case parseDisplay xs of
        (Left e) -> error (show e)
        (Right x) -> do
             socket <- cont x >>= return . fromMaybe
                 (error "couldn't open socket")
             auth <- getAuthInfo socket (display x) 
             hndl <- socketToHandle socket ReadWriteMode
             return (hndl, auth, x)
open xs = let
    cont (DispName p h d s)
        | null h || null p && h == "unix" = openUnix p
             ("/tmp/.X11-unix/X" ++ show d)
        | otherwise = openTCP p h (6000 + d)

    openTCP proto host port
        | proto == [] || proto == "tcp" =
            let addrInfo = defaultHints { addrFlags  = [ AI_ADDRCONFIG
                                                       , AI_NUMERICSERV
                                                       ]
                                        , addrFamily = AF_UNSPEC
                                        , addrSocketType = Stream
                                        }
                conn (AddrInfo _ fam socktype proto addr _) Nothing = do
                   fd <- socket fam socktype proto
                   connect fd addr
                   return $ Just fd
                conn _ x = return x
            in getAddrInfo (Just addrInfo) (Just host) (Just (show port))
               >>= foldrM conn Nothing
        | otherwise = error "'protocol' should be empty or 'tcp'"
 
    in case parseDisplay xs of
        (Left e) -> error (show e)
        (Right x) -> do
             socket <- cont x >>= return . fromMaybe
                 (error "couldn't open socket")
             auth <- getAuthInfo socket (display x) 
             hndl <- socketToHandle socket ReadWriteMode
             return (hndl, auth, x)