socketOutgoing :: WebappConfiguration -> Socket -> ChanInfo -> IO ()
socketOutgoing conf sock chans = do
    msg <- readChan (brokerRequestChan chans)
    let rawMessage = BS.toStrict . messagePut $ msg
    catchIOError (NBS.sendTo sock rawMessage (brokerSockAddr conf)) (errHandler (requestsAndResponsesToCenterChan chans) msg)
    socketOutgoing conf sock chans

    where errHandler :: Chan CenterRequestOrResponse -> WebappRequestMessage -> IOError -> IO Int -- if there is an error, an error message is sent back.
          errHandler bc msg _ = writeChan bc
            (BrokerCenterResponse $ defaultValue { Rp.type' = rqToRpType (Rq.type' msg),
                                                   Rp.sequence_number = Rq.sequence_number msg,
                                                   Rp.status = Just False,
                                                   Rp.error_message = Just $ uFromString "Couldn't reach broker",
                                                   Rp.error_code = Just $ fromIntegral 16 } ) >> return 0

-- Socket setup
socketOutgoing :: WebappConfiguration -> Socket -> ChanInfo -> IO ()
socketOutgoing conf sock chans = do
    msg <- readChan (brokerRequestChan chans)
    let rawMessage = BS.toStrict . messagePut $ msg
    catchIOError (NBS.sendTo sock rawMessage (brokerSockAddr conf)) (errHandler (requestsAndResponsesToCenterChan chans) msg)
    socketOutgoing conf sock chans

    where errHandler :: Chan CenterRequestOrResponse -> WebappRequestMessage -> IOError -> IO Int -- if there is an error, an error message is sent back.
          errHandler bc msg _ = writeChan bc
            (BrokerCenterResponse $ defaultValue { Rp.type' = rqToRpType (Rq.type' msg),
                                                   Rp.sequence_number = Rq.sequence_number msg,
                                                   Rp.status = Just False,
                                                   Rp.error_message = Just $ uFromString "Couldn't reach broker",
                                                   Rp.error_code = Just $ fromIntegral 16 } ) >> return 0

-- Socket setup
socketOutgoing conf sock chans = do
    msg <- readChan (brokerRequestChan chans)
    let rawMessage = BS.toStrict . messagePut $ msg
    catchIOError (NBS.sendTo sock rawMessage (brokerSockAddr conf)) (errHandler (requestsAndResponsesToCenterChan chans) msg)
    socketOutgoing conf sock chans

    where errHandler :: Chan CenterRequestOrResponse -> WebappRequestMessage -> IOError -> IO Int -- if there is an error, an error message is sent back.
          errHandler bc msg _ = writeChan bc
            (BrokerCenterResponse $ defaultValue { Rp.type' = rqToRpType (Rq.type' msg),
                                                   Rp.sequence_number = Rq.sequence_number msg,
                                                   Rp.status = Just False,
                                                   Rp.error_message = Just $ uFromString "Couldn't reach broker",
                                                   Rp.error_code = Just $ fromIntegral 16 } ) >> return 0

-- Socket setup