/// <summary>Updates interinstance replication state.</summary>
		public static void UpdateReplicationState(IPEndPoint localEndPoint, ReplicationSettings settings)
		{
			var localCouchDBUri = localEndPoint.ToHttpUri();
			Log.InfoFormat("Saving replication documents using server URI: {0}", localCouchDBUri);

			var couchApi = Factory.CreateCouchApi(localCouchDBUri);
			
			CreateDatabases(couchApi, settings.DatabasesToReplicate);

			var descriptorsToCreate = (
				from endpoint in settings.EndPointsToReplicateTo
				from dbName in settings.DatabasesToReplicate
				let endpointUri = endpoint.ToHttpUri()
				let descriptorId = GetDescriptorId(endpointUri, dbName)
				let descriptor = new ReplicationTaskDescriptor {
					Id = descriptorId,
					Continuous = true,
					Source = new Uri(endpointUri, dbName),
					Target = new Uri(dbName, UriKind.Relative),
					UserContext = new ReplicationUserContext {
						Roles = new[] { "admin" }
					}
				}
				select descriptor
			).ToArray();

			Log.InfoFormat(
				"Replication descriptors should be created: {0}", 
				string.Join(", ", descriptorsToCreate.Select(d => d.Id))
			);

			var replicatorApi = couchApi.Replicator;
			replicatorApi.GetAllDescriptorNames()
				.ContinueWith(
					t => {
						Log.Info("Loading existing replication descriptors...");
						var loadTasks = (
							from descriptorId in t.Result
							select replicatorApi
											.RequestDescriptorById(descriptorId)
											.ContinueWith(pt => pt.IsCompleted? pt.Result: null)
							).ToArray();
						return Task.Factory.ContinueWhenAll(
							loadTasks,
							tasks => {
								var existingDescriptors = (
									from loadTask in tasks
									select loadTask.Result
									into descriptor
									where descriptor != null
									select descriptor).ToArray();

								Log.InfoFormat(
									"{0} replication descriptors found in the _replicator database: {1}",
									existingDescriptors.Length,
									string.Join(", ", existingDescriptors.Select(d => d.Id)));
								return existingDescriptors;
							});
					})
				.Unwrap()
				.ContinueWith(
					t => {
						var descritorsToDelete = t.Result;
						Log.InfoFormat(
							"Deleting {0} replication descriptors from _replicator database: {1}",
							descritorsToDelete.Length,
							string.Join(", ", descritorsToDelete.Select(d => d.Id)));
						var deleteTasks =
							from descritorToDelete in descritorsToDelete
							select replicatorApi.DeleteDescriptor(descritorToDelete);
						return Task.Factory.ContinueWhenAll(deleteTasks.ToArray(), ThrowAggregateIfFaulted);
					})
				.Unwrap()
				.ContinueWith(
					t => {
						Log.InfoFormat(
							"Creating {0} replication descriptors in _replicator database: {1}",
							descriptorsToCreate.Length,
							string.Join(", ", descriptorsToCreate.Select(d => d.Id))
							);
						var createDescriptorsTasks = (
							from descriptorToCreate in descriptorsToCreate
							select replicatorApi.SaveDescriptor(descriptorToCreate)
							).ToArray();
						return createDescriptorsTasks.Length == 0
							? Task.Factory.StartNew(() => { })
							: Task.Factory.ContinueWhenAll(createDescriptorsTasks, ThrowAggregateIfFaulted);
					})
				.Unwrap()
				.Wait();

			Log.Info("Replication descriptors have been created");
		}