def attach(self, host=None, source=None, sourcetype=None):
        """Opens a stream (a writable socket) for writing events to the index.

        :param host: The host value for events written to the stream.
        :type host: ``string``
        :param source: The source value for events written to the stream.
        :type source: ``string``
        :param sourcetype: The sourcetype value for events written to the
            stream.
        :type sourcetype: ``string``

        :return: A writable socket.
        """
        args = { 'index': self.name }
        if host is not None: args['host'] = host
        if source is not None: args['source'] = source
        if sourcetype is not None: args['sourcetype'] = sourcetype
        path = UrlEncoded(PATH_RECEIVERS_STREAM + "?" + urllib.urlencode(args), skip_encode=True)

        # Since we need to stream to the index connection, we have to keep
        # the connection open and use the Splunk extension headers to note
        # the input mode
        sock = self.service.connect()
        headers = ["POST %s HTTP/1.1\r\n" % self.service._abspath(path),
                   "Host: %s:%s\r\n" % (self.service.host, int(self.service.port)),
                   "Accept-Encoding: identity\r\n",
                   "Authorization: %s\r\n" % self.service.token,
                   "X-Splunk-Input-Mode: Streaming\r\n",
                   "\r\n"]
        for h in headers:
            sock.write(h)
        return sock