diff --git a/app/Main.hs b/app/Main.hs
--- a/app/Main.hs
+++ b/app/Main.hs
@@ -1,5 +1,6 @@
 module Main where
 
+import Control.Concurrent (forkIO, newEmptyMVar, takeMVar)
 import Network.Eventsourced.Lib (application)
 import Network.Eventsourced.Args (Options(..), getCommandLineOptions)
 
@@ -9,10 +10,13 @@
 main :: IO ()
 main = do
     args <- getArgs
+    shutdownMVar <- newEmptyMVar
     let opts = getCommandLineOptions args
     case opts of
         Options _    _          (Just help)  -> putStrLn help
         Options port allowOrigin Nothing     -> do
             putStrLn $ "Streaming standard input to port " ++ (show port)
             putStrLn $ "Allowed orgins: " ++ allowOrigin
-            run port $ application allowOrigin
+            let app = application shutdownMVar allowOrigin
+            _ <- forkIO $ run port app
+            takeMVar shutdownMVar
diff --git a/eventsourced.cabal b/eventsourced.cabal
--- a/eventsourced.cabal
+++ b/eventsourced.cabal
@@ -1,5 +1,5 @@
 name:                eventsourced
-version:             1.0.0.0
+version:             1.1.0.0
 synopsis:            Server-Sent Events the UNIX way
 description:
   @eventsourced@ streams stdin to a TCP\/IP port as @text\/event-source@.
diff --git a/src/Network/Eventsourced/Lib.hs b/src/Network/Eventsourced/Lib.hs
--- a/src/Network/Eventsourced/Lib.hs
+++ b/src/Network/Eventsourced/Lib.hs
@@ -1,5 +1,6 @@
 module Network.Eventsourced.Lib (application, serverEvent, createCorsHeaders) where
 
+import Control.Concurrent (MVar, putMVar)
 import System.IO.Error (tryIOError)
 import Network.Wai (Middleware, Application)
 import Network.Wai.EventSource (ServerEvent(..), eventSourceAppIO)
@@ -12,10 +13,15 @@
 serverEvent (Left _) = CloseEvent
 serverEvent (Right s) = ServerEvent Nothing Nothing [ fromString s ]
 
-eventFromLine :: IO ServerEvent
-eventFromLine = do
+eventFromLine :: MVar () -> IO ServerEvent
+eventFromLine shutdownMVar = do
     input <- tryIOError getLine
-    return $ serverEvent input
+    let event = serverEvent input
+    case event of
+        CloseEvent -> do
+            putMVar shutdownMVar ()
+            return event
+        _ -> return event
 
 createCorsHeaders :: String -> [(ByteString, ByteString)]
 createCorsHeaders s = [(pack "Access-Control-Allow-Origin", pack s)]
@@ -23,5 +29,7 @@
 addCorsHeaders :: String -> Middleware
 addCorsHeaders s = addHeaders $ createCorsHeaders s
 
-application :: String -> Application
-application allowOrigin = addCorsHeaders allowOrigin $ eventSourceAppIO eventFromLine
+application :: MVar () -> String -> Application
+application shutdown allowOrigin = do
+    let app = eventSourceAppIO $ eventFromLine shutdown
+    addCorsHeaders allowOrigin app
