<commit_before>var net = require('net');
var util=require('util');
var EventEmitter = require('events');
var Logger=require('../util/Logger');

var Connection = function (options) {
    if(options && options.name && options.host && options.port){
      this.name = options.name;
      this.host = options.host;
      this.port = options.port;  
    }
    else{
      throw new Error("Connection name,host, and port parameters are all required!")
    }
    
    // Initialize necessary properties from `EventEmitter` in this instance
    EventEmitter.call(this);

    this.connect = function () {
      Logger.info('Attempting to connect to '+this.name+' at '+this.host+':'+this.port);
      this.socket.connect(this.port, this.host);
    };

    this.disconnect=function(){
      Logger.info('Disconnecting from '+this.name+' at '+this.host+':'+this.port);
      this.socket.close();
    };

    this.reconnect=function(timeout,attempts){
      this.disconnect();      
      this.connect();
    };

    this.write=function(data){
      this.socket.write(data,'utf8',function(){
        this.emit('write',data);
        Logger.info("Network "+this.name+" Sent: " + data);
      }.bind(this));
    };

    this.socket = new net.Socket();

    this.socket.on('connect',function(){
      this.emit('connect');
      Logger.info('Sucessfully connected to '+this.name+' with host '+this.host+' and port '+this.port);
    }.bind(this));

    this.socket.on('error',function(error){ <commit_msg>Detect if error before emitting write event for connection<commit_after>var net = require('net');
var util=require('util');
var EventEmitter = require('events');
var Logger=require('../util/Logger');

var Connection = function (options) {
    if(options && options.name && options.host && options.port){
      this.name = options.name;
      this.host = options.host;
      this.port = options.port;  
    }
    else{
      throw new Error("Connection name,host, and port parameters are all required!")
    }
    
    // Initialize necessary properties from `EventEmitter` in this instance
    EventEmitter.call(this);

    this.connect = function () {
      Logger.info('Attempting to connect to '+this.name+' at '+this.host+':'+this.port);
      this.socket.connect(this.port, this.host);
    };

    this.disconnect=function(){
      Logger.info('Disconnecting from '+this.name+' at '+this.host+':'+this.port);
      this.socket.close();
    };

    this.reconnect=function(timeout,attempts){
      this.disconnect();      
      this.connect();
    };

    this.write=function(data){
      this.socket.write(data,'utf8',function(error){
        if(!error){
          this.emit('write',data);
          Logger.info("Network "+this.name+" Sent: " + data);
        }
      }.bind(this));
    };

    this.socket = new net.Socket();

    this.socket.on('connect',function(){
      this.emit('connect');
      Logger.info('Sucessfully connected to '+this.name+' with host '+this.host+' and port '+this.port);
    }.bind(this));

    this.socket.on('error',function(error){ 
{'fraction_non_alphanumeric': 0.16830065359477125, 'fraction_numerical': 0.00065359477124183, 'mean_word_length': 3.0758988015978694, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2065062', 'n_tokens_mistral': 931, 'n_tokens_neox': 904, 'n_words': 189}