reconnect :: MIrc -> IO (Either IOError MIrc)
reconnect mIrc = try $ do
  server <- readMVar mIrc

  h <- connectTo (B.unpack $ sAddr server) (PortNumber $ fromIntegral $ sPort server)
  hSetBuffering h NoBuffering
  modifyMVar_ mIrc (\s -> return $ s {sSock = Just h})

  -- Initialize connection with the server
  _ <- withMVar mIrc greetServer

  -- Restart the listen loop.
  listenId <- forkIO (listenLoop mIrc)
  cmdId <- forkIO (execCmdsLoop mIrc)
  modifyMVar_ mIrc (\s -> return $ s {sListenThread = Just listenId,
                        sCmdThread = Just cmdId})
  return mIrc

{-
-- |Reconnects to the server.
reconnect :: MIrc -> IO (Either IOError MIrc)
reconnect server = do
  s <- readMVar server

  let conf = IrcConfig (B.unpack $ sAddr s) (sPort s)
                       (B.unpack $ sNickname s) (B.unpack $ sUsername s)
                       (B.unpack $ sRealname s) (map (B.unpack) (sChannels s))
                       (elems $ sEvents s) (sCTCPVersion s) (sCTCPTime s)
  connect conf True (sDebug s)
-}
reconnect :: MIrc -> IO (Either IOError MIrc)
reconnect mIrc = try $ do
  server <- readMVar mIrc

  h <- connectTo (B.unpack $ sAddr server) (PortNumber $ fromIntegral $ sPort server)
  hSetBuffering h NoBuffering
  modifyMVar_ mIrc (\s -> return $ s {sSock = Just h})

  -- Initialize connection with the server
  _ <- withMVar mIrc greetServer

  -- Restart the listen loop.
  listenId <- forkIO (listenLoop mIrc)
  cmdId <- forkIO (execCmdsLoop mIrc)
  modifyMVar_ mIrc (\s -> return $ s {sListenThread = Just listenId,
                        sCmdThread = Just cmdId})
  return mIrc

{-
-- |Reconnects to the server.
reconnect :: MIrc -> IO (Either IOError MIrc)
reconnect server = do
  s <- readMVar server

  let conf = IrcConfig (B.unpack $ sAddr s) (sPort s)
                       (B.unpack $ sNickname s) (B.unpack $ sUsername s)
                       (B.unpack $ sRealname s) (map (B.unpack) (sChannels s))
                       (elems $ sEvents s) (sCTCPVersion s) (sCTCPTime s)
  connect conf True (sDebug s)
-}
reconnect mIrc = try $ do
  server <- readMVar mIrc

  h <- connectTo (B.unpack $ sAddr server) (PortNumber $ fromIntegral $ sPort server)
  hSetBuffering h NoBuffering
  modifyMVar_ mIrc (\s -> return $ s {sSock = Just h})

  -- Initialize connection with the server
  _ <- withMVar mIrc greetServer

  -- Restart the listen loop.
  listenId <- forkIO (listenLoop mIrc)
  cmdId <- forkIO (execCmdsLoop mIrc)
  modifyMVar_ mIrc (\s -> return $ s {sListenThread = Just listenId,
                        sCmdThread = Just cmdId})
  return mIrc

{-
-- |Reconnects to the server.
reconnect :: MIrc -> IO (Either IOError MIrc)
reconnect server = do
  s <- readMVar server

  let conf = IrcConfig (B.unpack $ sAddr s) (sPort s)
                       (B.unpack $ sNickname s) (B.unpack $ sUsername s)
                       (B.unpack $ sRealname s) (map (B.unpack) (sChannels s))
                       (elems $ sEvents s) (sCTCPVersion s) (sCTCPTime s)
  connect conf True (sDebug s)
-}