 
 
 // void  write_into_the_database(DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t b[],  DB_t final_CW, bool party, size_t db_nitems)
 // {

 // 	//DB_t tmp = 0;
 // 	if(!party) final_CW = -final_CW;
 // 	for(size_t j = 0; j < db_nitems; ++j)
 // 	{
 // 		// if(flags[j] != 0) 
 // 		// {
 // 		//  tmp = final_CW;
 // 		// }
 // 		// else
 // 		// {
 // 		//  tmp = 0;
 // 		// }
 // 		DB[j] = DB[j] + b[j]  + ((flags[j] != 0) ? final_CW : 0);;

	// 	// if(!party) updated_DB[j] = DB[j] + b[j]  - tmp;
 // 	//     if(party)  updated_DB[j] = DB[j] + b[j]  + tmp; 	    
 // 	}
 // }
 void  write_into_the_database(DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t b[],  DB_t final_CW, bool party, size_t db_nitems, size_t rotate_by = 0)
 {

 //	if(!party) final_CW = -final_CW;
	
	//std::cout << "rotate_by = " << rotate_by << std::endl;
//	std::cout << "final_CW =  " << final_CW << std::endl;
 	for(size_t j = 0; j < db_nitems; ++j)
 	{ 
// 	  DB[j] = DB[j] + b[(j + rotate_by) % db_nitems]  + ((flags[(j + rotate_by) % db_nitems] != 0) ? final_CW : 0);   
	  const size_t ind = (j + rotate_by) % db_nitems;
	  
	  //DB_t tmp =  ( flags[(j + rotate_by) % db_nitems]   * final_CW);
		DB_t tmp = (flags[ind] * final_CW);
	   //std::cout << j <<  " --->> tmp = " << tmp << " ---- " << (int)flags[(j + rotate_by) % db_nitems] << " * " << final_CW <<  "----> ind = " << ind <<  std::endl;
		DB[j] = DB[j] + b[ind]  + tmp;     
		updated_DB[j] = DB[j];
	   //DB[j] = DB[j] + b[(j + rotate_by) % db_nitems]  + tmp;     
 	}
 }
void duoram_update(tcp::socket& in2, tcp::socket& sb, DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t standard_basis[], DB_t M0, DB_t alpha0, bool party, size_t db_nitems, size_t offset = 0)
{	 	

	DB_t tmp = M0 + alpha0;	
	DB_t recv_final_cw; 
	write(sb, boost::asio::buffer(&tmp, sizeof(tmp)));
	write(in2, boost::asio::buffer(&tmp, sizeof(tmp)));
	read(sb, boost::asio::buffer(&recv_final_cw, sizeof(recv_final_cw)));
 
	recv_final_cw = recv_final_cw + tmp;
   
	write_into_the_database(DB, updated_DB, flags, standard_basis, recv_final_cw, party, db_nitems, offset);


}


void reconstruct_database(tcp::socket& sb, DB_t DB[], const size_t db_nitems)
{
		for(size_t j = 0; j < db_nitems; ++j)
		{
			DB_t DB_j;
			boost::asio::write(sb, boost::asio::buffer(&DB[j], sizeof(DB[j])));
			boost::asio::read(sb, boost::asio::buffer(&DB_j, sizeof(DB_j)));
			DB_j = DB_j + DB[j];
			if(DB_j != 0) std::cout << j << " -> " << DB_j << std::endl;
		}
}


auto DuoramUpdate(bool party, size_t db_nitems,   size_t rotate_by_,
				  DB_t DB[], DB_t updated_DB[], int8_t writing_b[], DB_t b[],  DB_t final_CW, DB_t message,
				  int8_t writing_c[], int8_t writing_d[], DB_t c[], DB_t d[])
{

			// final_CW = final_CW + message;
			// read_flags(party, db_nitems);
		 	
		    // rotate_by_[w] = WritePb_[w].shift + WritePb_recv[w].shift;		 			
			
			// std::cout << "rotate_by_[w] = " << rotate_by_[w] << std::endl;
   			
			  write_into_the_database(DB, updated_DB, writing_b, b, final_CW, party, db_nitems, - rotate_by_);
			
			#ifdef ThreeParty
				if(!party) duoram_refresh(writing_b, writing_c, writing_d, b, c, d,  final_CW, party, db_nitems, -rotate_by_); 			
				if( party) duoram_refresh(writing_b, writing_d, writing_c, b, d, c,  final_CW, party, db_nitems, -rotate_by_); 
			#endif
}