title: How to properly close network connections with network-conduit?
body: <p>
  To learn the basics of <code>conduit</code> library, I used <a href="http://hackage.haskell.org/package/network-conduit" rel="nofollow"><code>network-conduit</code></a> to make a simple echo server:</p>
  
  <pre class="lang-hs prettyprint-override"><code>import Control.Monad.IO.Class
  import qualified Data.ByteString.Char8 as BS
  import Data.Conduit
  import Data.Conduit.Network
  
  -- A conduit that print the input it receives on the console
  -- and passes it through.
  echo :: (MonadIO m) =&gt; Conduit BS.ByteString m BS.ByteString
  echo = do
      yield (BS.pack "Anything you type will be echoed back.\n")
      -- Print the received data to the console as well:
      awaitForever (\x -&gt; liftIO (BS.putStr x) &gt;&gt; yield x)
  
  echoApp :: (MonadIO m) =&gt; Application m
  echoApp appData = appSource appData $= echo $$ appSink appData
  
  -- Listen on port 4545:
  main :: IO ()
  main = runTCPServer (serverSettings 4545 HostAny) echoApp
  </code></pre>
  
  <p>It does what I wanted, but when the client closes its part of the connection, the server is still waiting for input instead of writing out any remaining data and closing its sending part of the connection too:</p>
  
  <pre class="lang-sh prettyprint-override"><code>$ nc localhost 4545 &lt;&lt;&lt;"Hello world!"
  Anything you type will be echoed back.
  Hello world!
  </code></pre>
  
  <p>I tried removing <code>echo</code> and do just</p>
  
  <pre class="lang-hs prettyprint-override"><code>echoApp appData = appSource appData $$ appSink appData
  </code></pre>
  
  <p>but the problem is still there. What am I doing wrong?</p>