start :: Throttle -> IO ()
start Throttle{..} = withSocketsDo $ do
  c <- newTeller logging
  listener <- listenOn (PortNumber . fromIntegral $ listen)
  forever $ do
    (client,_) <- accept listener
    tell c $ [show client,": New connection on port ",show port]
    _ <- forkIO $ do
      server <- connectToServer
      tell c $ [show client,": ",show server,": Connected to server at "
               ,host,":",show port, " with delay ", show delay]
      let proxyTo f t = forkIO (proxyToWithChan c f t) >> return ()
      client `proxyTo` server
      server `proxyTo` client
    return ()
  where connectToServer = do
          addrinfos <- getAddrInfo Nothing (Just host) (Just $ show port)
          let serveraddr = head addrinfos
          server <- socket (addrFamily serveraddr) Stream defaultProtocol
          connect server (addrAddress serveraddr)
          return server

        proxyToWithChan c from to = do 
          mapData from to
          close c from to
          return ()

        mapData from to = do
          msg <- recv from bufferSize
          unless (B.null msg) $ do
            _ <- sendAll to msg
            unless (speed == 0) $ threadDelay delay
            mapData from to

        bufferSize = 1024 * 4 :: Int
        delay = round $ (fromIntegral $ (1000*1000) * bufferSize) / (speed * 1024)
                    

        close c a b = do
          tell c $ [show a,":",show b,": Closing connections."]
          sClose a
          sClose b


-- | Create a new console logger.
start :: Throttle -> IO ()
start Throttle{..} = withSocketsDo $ do
  c <- newTeller logging
  listener <- listenOn (PortNumber . fromIntegral $ listen)
  forever $ do
    (client,_) <- accept listener
    tell c $ [show client,": New connection on port ",show port]
    _ <- forkIO $ do
      server <- connectToServer
      tell c $ [show client,": ",show server,": Connected to server at "
               ,host,":",show port, " with delay ", show delay]
      let proxyTo f t = forkIO (proxyToWithChan c f t) >> return ()
      client `proxyTo` server
      server `proxyTo` client
    return ()
  where connectToServer = do
          addrinfos <- getAddrInfo Nothing (Just host) (Just $ show port)
          let serveraddr = head addrinfos
          server <- socket (addrFamily serveraddr) Stream defaultProtocol
          connect server (addrAddress serveraddr)
          return server

        proxyToWithChan c from to = do 
          mapData from to
          close c from to
          return ()

        mapData from to = do
          msg <- recv from bufferSize
          unless (B.null msg) $ do
            _ <- sendAll to msg
            unless (speed == 0) $ threadDelay delay
            mapData from to

        bufferSize = 1024 * 4 :: Int
        delay = round $ (fromIntegral $ (1000*1000) * bufferSize) / (speed * 1024)
                    

        close c a b = do
          tell c $ [show a,":",show b,": Closing connections."]
          sClose a
          sClose b


-- | Create a new console logger.
start Throttle{..} = withSocketsDo $ do
  c <- newTeller logging
  listener <- listenOn (PortNumber . fromIntegral $ listen)
  forever $ do
    (client,_) <- accept listener
    tell c $ [show client,": New connection on port ",show port]
    _ <- forkIO $ do
      server <- connectToServer
      tell c $ [show client,": ",show server,": Connected to server at "
               ,host,":",show port, " with delay ", show delay]
      let proxyTo f t = forkIO (proxyToWithChan c f t) >> return ()
      client `proxyTo` server
      server `proxyTo` client
    return ()
  where connectToServer = do
          addrinfos <- getAddrInfo Nothing (Just host) (Just $ show port)
          let serveraddr = head addrinfos
          server <- socket (addrFamily serveraddr) Stream defaultProtocol
          connect server (addrAddress serveraddr)
          return server

        proxyToWithChan c from to = do 
          mapData from to
          close c from to
          return ()

        mapData from to = do
          msg <- recv from bufferSize
          unless (B.null msg) $ do
            _ <- sendAll to msg
            unless (speed == 0) $ threadDelay delay
            mapData from to

        bufferSize = 1024 * 4 :: Int
        delay = round $ (fromIntegral $ (1000*1000) * bufferSize) / (speed * 1024)
                    

        close c a b = do
          tell c $ [show a,":",show b,": Closing connections."]
          sClose a
          sClose b


-- | Create a new console logger.