var Asyncplify = require('asyncplify');
var debug = require('debug')('asyncplify-mssql:connect');
var Connection = require('./connection');
var SqlConnection = require('mssql').Connection;

function Connect(config, sink) {
	this.conn = new SqlConnection(config);
	this.sink = sink;
	this.sink.source = this;

	debug('connecting to %s on %s', config.database, config.server);

	var self = this;

	this.conn.connect(function connect(err) {
		if (self.sink) {
			if (err) {
				debug('error connecting on %s', config.server);
				debug(err);
			} else {
				debug('connected to %s on %s', config.database, config.server);
			}

			self.sink.emit(new Connection(self.conn));
			self.conn = null;

			if (self.sink) self.sink.end(err);
		} else if (self.conn) {
			self.conn.close();
			self.conn = null;
		}
	});
}

Connect.prototype.setState = function (state) {
	if (state === Asyncplify.states.CLOSED) {
		this.sink = null;
		if (this.conn) this.conn.close();
		this.conn = null;
	}
};

module.exports = function (config) {
	return new Asyncplify(Connect, config);
};
@@ -617,13 +617,8 @@
 );%0A%09
-%09%09%7D%0A%0A
 %09%09%09s
@@ -659,16 +659,22 @@
 conn));%0A
+%09%09%09%7D%0A%0A
 %09%09%09self.
@@ -686,17 +686,16 @@
 = null;%0A
-%0A
 %09%09%09if (s

Return connection error into the asyncplify stream