 	{
 		//Fetch public file listing from mysqldb
 		private var c:Communications;
-		private var mysqlConnection:Connection;
 		private var fileSharingInfo:File;
 		
 		private var _filePaths:Vector.<String>;

				c.addEventListener(NetworkGroupEvent.CONNECTION_SUCCESSFUL, handleGroupConnectionSuccessful);
				c.addEventListener(NetworkGroupEvent.CONNECTION_FAILED, handleGroupConnectionFailed);
			}
		}
		
		private function handleGroupConnectionFailed(e:NetworkGroupEvent):void 

			}
			
			//Get the last row in the table
			
			var st:SQLStatement = new SQLStatement();
			st.text = "SELECT * FROM files "
				+ "ORDER BY `id` DESC "
				+ "LIMIT 1;";
			var result = c.databaseService.readFromDB(SERVICE_NAME, st);
			
			if (result is SQLError)
			{
				trace("MYSQL ERROR: " + result);
				dispatchEvent(new FileSharingEvent(FileSharingEvent.ERROR, result, file.name));
				return;
			}
			
			if ((result as SQLResult).data[0] == null)
			{
				lastEndIndex = 0;
			}
			else
			{
				var lastEndIndex:Number = (result as SQLResult).data[0].groupID;
			}
			
			
			var numberOfSplits:Number = Math.ceil(file.size / SPLIT_SIZE);
			
			if (lastEndIndex + numberOfSplits > 9007199254740992)
			{
				//A new group must be made. Tell all clients.
				numberOfGroups ++;
			}
			var startIndex:Number = lastEndIndex + 1;
			var endIndex:Number = startIndex + numberOfSplits;
			
			//write to sql --- 
			
			//generate md5s
			var md5s:String = new String()
			var fileData:ByteArray = new ByteArray();
			fs.open(file, FileMode.READ);
				for (var i:Number = 0; i < numberOfSplits; i++)
				{
					var tmp:ByteArray = new ByteArray();
					
					var toRead:Number = SPLIT_SIZE;
					if (fs.bytesAvailable < SPLIT_SIZE)
					{
						toRead = fs.bytesAvailable;
					}
					
					fs.readBytes(tmp, 0, toRead);
					var newMd5:String = MD5.hashBytes(tmp);
					md5s = md5s + newMd5;
					trace("MD5 HASH:" + newMd5 + " for file sector " + i.toString());
				}
			fs.close();
			
			//Mysql format: id(autogen) | FileName.ext | Start Index | EndIndex |  groupID | md5
			var submitSt:SQLStatement = new SQLStatement();
			submitSt.text = "INSERT INTO files (`filename`, `startindex`, `endindex`, `groupid`, `md5`)"
				+ " VALUES ('" + file.name + "'," + startIndex.toString() + "," + endIndex.toString() + "," + numberOfGroups.toString() + ",'" + md5s + "');";
				
			trace("Submitting file to p2p database..");
			var submitResult = c.databaseService.writeToDB(SERVICE_NAME, submitSt);
			
			if (submitResult is SQLError)
			{
				trace("Final submit failure. Mysql Error: " + submitResult);
				dispatchEvent(new FileSharingEvent(FileSharingEvent.ERROR, "Failed to added file " + file.name + ". Couldn't submit to database..."));
				return;
			}
			
			trace("Writing to group..");
			//write to group
			c.addHaveObject(SERVICE_NAME + numberOfGroups.toString(), startIndex, endIndex);
			
			//write to index
			trace("Writing to index..");
			for (var i:Number = startIndex; i < endIndex; i++)
			{
				_filePaths.push(file.nativePath);
				_fileIDs.push(i);
				_groupIDs.push(numberOfGroups);
				_fileStartIndex.push(startIndex);
				_fileEndIndex.push(endIndex);
			}
			
			trace("Writing to filesharing info..");
			//write to filesharinginfo
			var fs:FileStream = new FileStream();
			
			fs.open(fileSharingInfo, FileMode.WRITE);
				fs.writeFloat(new Number(_filePaths.length)) //Write number of files to first float
				fs.close();
			fs.open(fileSharingInfo, FileMode.APPEND);
				fs.writeUTF(file.nativePath);
				fs.writeFloat(numberOfGroups);
				fs.writeFloat(lastEndIndex + 1);
				fs.close();
			
			dispatchEvent(new FileSharingEvent(FileSharingEvent.FILE_ADDED, "Successfully added file " + file.name, file.nativePath, startIndex, endIndex, numberOfGroups));
		}
		
		
